<?php
/* * ***************************************************************************
 * @author:HW
 * Email:phamhung.ktxclc@gmail.com
 * **************************************************************************** */

load_theme_textdomain('AuctionTheme', TEMPLATEPATH . '/languages');

DEFINE("AUCTIONTHEME_VERSION", "4.5.3");
DEFINE("AUCTIONTHEME_RELEASE", "02 November 2012");

global $default_search;
$default_search = __("Begin to search by typing here...", 'AuctionTheme');

//---------------------------------- 

add_theme_support('post-thumbnails');


global $current_theme_locale_name;
$current_theme_locale_name = 'AuctionTheme';

global $category_url_link, $location_url_link, $auctions_url_thing, $auction_thing_list;
$category_url_link = get_option("AuctionTheme_category_permalink");
$location_url_link = get_option("AuctionTheme_location_permalink");
$auctions_url_thing = get_option("AuctionTheme_auction_permalink");
$auction_thing_list = "auction-list";

if (empty($category_url_link))
    $category_url_link = 'section';
if (empty($location_url_link))
    $location_url_link = 'location';
if (empty($auctions_url_thing))
    $auctions_url_thing = 'auctions';

//----------------------------------

include 'autosuggest.php';
include 'lib/first_run_emails.php';
include 'lib/admin_menu.php';
include 'my-upload.php';
include 'lib/first_run.php';
include 'lib/post_new.php';
include 'lib/login_register/custom2.php';

include 'lib/widgets/browse-by-category.php';
include 'lib/widgets/browse-by-location.php';
include 'lib/widgets/latest-posted-auctions.php';
include 'lib/widgets/most-visited-auctions.php';
include 'lib/widgets/ending-soonest-auctions.php';
include 'lib/widgets/ending-soonest-auctions-big.php';
include 'lib/widgets/best-rated-users.php';
include 'lib/widgets/featured-auctions.php';
include 'lib/widgets/latest-featured-auctions-big.php';

include 'lib/my_account/my_account.php';
include 'lib/my_account/personal_info.php';
include 'lib/my_account/private_messages.php';
include 'lib/my_account/feedback.php';
include 'lib/my_account/payments.php';
include 'lib/my_account/won_items.php';
include 'lib/my_account/not_won_items.php';
include 'lib/my_account/shipped_items.php';
include 'lib/my_account/paid_n_shipped.php';
include 'lib/my_account/outstanding_payments.php';
include 'lib/my_account/items_i_bid.php';
include 'lib/my_account/active_auctions.php';
include 'lib/my_account/sold_items.php';
include 'lib/my_account/closed_auctions.php';
include 'lib/my_account/unpublished_auctions.php';
include 'lib/my_account/awaiting_payments.php';
include 'lib/my_account/no_shipped_items.php';
include 'lib/my_account/pay_for_item.php';
include 'lib/my_account/pay_item_by_credits.php';
include 'lib/my_account/paid_items.php';


include 'lib/all_categories.php';
include 'lib/all_locations.php';
include 'lib/advanced_search.php';
include 'lib/watch_list.php';
include 'lib/cronjob.php';
include 'lib/blog_page.php';
include 'lib/social/social.php';

//----------------------------------

add_action('admin_menu', 'AuctionTheme_admin_main_menu_scr');
add_action('admin_head', 'AuctionTheme_admin_main_head_scr');
add_action('init', 'AuctionTheme_create_post_type');
add_action('widgets_init', 'AuctionTheme_framework_init_widgets');
add_action('wp_head', 'auctionTheme_coin_slider_home');
add_action("template_redirect", 'auctionTheme_template_redirect');
add_action('query_vars', 'AuctionTheme_add_query_vars');
add_action('save_post', 'auctionTheme_save_custom_fields');
add_filter('wp_head', 'AuctionTheme_add_max_nr_of_images');

add_action('the_content', 'AuctionTheme_blog_page_look_for_stuff');
add_action('the_content', 'AuctionTheme_all_categories_look_for_stuff');
add_action('the_content', 'AuctionTheme_all_locations_look_for_stuff');
add_action('the_content', 'AuctionTheme_adv_search_look_for_stuff');
add_action('the_content', 'AuctionTheme_post_new_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_pay_item_by_credits_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_pers_info_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_payments_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_priv_mess_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_reviews_look_for_stuff');
add_action('the_content', 'AuctionTheme_watch_list_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_active_auctions_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_sold_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_closed_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_unpub_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_aw_pay_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_not_shipped_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_shipped_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_pay_4_item_look_for_stuff');

add_action('the_content', 'AuctionTheme_my_account_won_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_NO_won_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_I_bid_items_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_outstanding_payment_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_paid_n_shipped_look_for_stuff');
add_action('the_content', 'AuctionTheme_my_account_paid__items_look_for_stuff');

add_filter('AuctionTheme_step1_after_private_bids_field', 'AuctionTheme_display_sealed_bidding_notice');
add_filter('AuctionTheme_step1_after_featured_field', 'AuctionTheme_display_featured_bidding_notice');

add_action("manage_posts_custom_column", "auctionTheme_my_custom_columns");
add_filter("manage_edit-auction_columns", "auctionTheme_my_auctions_columns");
add_action('generate_rewrite_rules', 'AuctionTheme_rewrite_rules');
add_action('wp_enqueue_scripts', 'auctionTheme_add_theme_styles');
add_action('init', 'AuctionTheme_register_my_menus');
add_filter('wp_head', 'AuctionTheme_set_cnt_down_vals', 1);

function AuctionTheme_set_cnt_down_vals() {
    ?>

    <script>
    	
        var AT_labels 	= ['<?php _e('Years', 'AuctionTheme'); ?>', 
            '<?php _e('Months', 'AuctionTheme'); ?>', 
            '<?php _e('Weeks', 'AuctionTheme'); ?>', 
            '<?php _e('Days', 'AuctionTheme'); ?>', 
            '<?php _e('Hrs', 'AuctionTheme'); ?>', 
            '<?php _e('Min', 'AuctionTheme'); ?>', 
            '<?php _e('Sec', 'AuctionTheme'); ?>'];
    		
        var AT_labels1 	= ['<?php _e('Year', 'AuctionTheme'); ?>', 
            '<?php _e('Month', 'AuctionTheme'); ?>', 
            '<?php _e('Week', 'AuctionTheme'); ?>', 
            '<?php _e('Day', 'AuctionTheme'); ?>', 
            '<?php _e('Hr', 'AuctionTheme'); ?>', 
            '<?php _e('Min', 'AuctionTheme'); ?>', 
            '<?php _e('Sec', 'AuctionTheme'); ?>'];

    </script>

    <?php
}

function AuctionTheme_adv_search_featured_ac() {
    $using_perm = AuctionTheme_using_permalinks();
    if ($using_perm)
        return get_permalink(get_option('AuctionTheme_adv_search_id')) . "?featured=1";
    else
        return get_bloginfo('siteurl') . "/?page_id=" . get_option($page_id) . "&featured=1";
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_is_able_to_post_auctions() {
    $opt = get_option('AuctionTheme_only_admins_post_auctions');
    if ($opt == "yes") {
        if (current_user_can('manage_options'))
            return true;
        else
            return false;
    }

    return true;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function auction_isValidEmail($email) {
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_pay4project_page_url($pid) {
    return get_bloginfo('siteurl');
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_auction_clear_table($spm = '') {
    return '<tr><td colspan="' . $spm . '"></td></tr>';
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_total_nr_of_auction() {
    $query = new WP_Query("post_type=auction&order=DESC&orderby=id&posts_per_page=-1&paged=1");
    return $query->post_count;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_total_nr_of_open_auction() {
    $query = new WP_Query("meta_key=closed&meta_value=0&post_type=auction&order=DESC&orderby=id&posts_per_page=-1&paged=1");
    return $query->post_count;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_total_nr_of_closed_auction() {
    $query = new WP_Query("meta_key=closed&meta_value=1&post_type=auction&order=DESC&orderby=id&posts_per_page=-1&paged=1");
    return $query->post_count;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_register_my_menus() {
    register_nav_menu('primary-auctiontheme-header', 'AuctionTheme top-header Menu');
    register_nav_menu('primary-auction-main-header', 'AuctionTheme Big Main Menu');
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_display_featured_bidding_notice() {
    $AuctionTheme_new_auction_feat_listing_fee = get_option('AuctionTheme_new_auction_feat_listing_fee');
    if (!empty($AuctionTheme_new_auction_feat_listing_fee)) {
        if ($AuctionTheme_new_auction_feat_listing_fee > 0) {
            printf(__('Extra fee of %s is applied with this option.', 'AuctionTheme'), auctionTheme_get_show_price($AuctionTheme_new_auction_feat_listing_fee));
        }
    }
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_display_sealed_bidding_notice() {
    $AuctionTheme_new_auction_sealed_bidding_fee = get_option('AuctionTheme_new_auction_sealed_bidding_fee');
    if (!empty($AuctionTheme_new_auction_sealed_bidding_fee)) {
        if ($AuctionTheme_new_auction_sealed_bidding_fee > 0) {
            printf(__('Extra fee of %s is applied with this option.', 'AuctionTheme'), auctionTheme_get_show_price($AuctionTheme_new_auction_sealed_bidding_fee));
        }
    }
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_browse__special_pg_link($page_id, $page) {
    $using_perm = AuctionTheme_using_permalinks();
    if ($using_perm)
        return get_permalink(get_option($page_id)) . "?pj=" . $page;
    else
        return get_bloginfo('siteurl') . "/?page_id=" . get_option($page_id) . "&pj=" . $page;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_field_tp($nr) {
    if ($nr == "1")
        return "Text field";
    if ($nr == "2")
        return "Select box";
    if ($nr == "3")
        return "Radio Buttons";
    if ($nr == "4")
        return "Check-box";
    if ($nr == "5")
        return "Large text-area";
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function auctionTheme_search_into($custid, $val) {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_custom_relations where custid='$custid'";
    $r = $wpdb->get_results($s);

    if (count($r) == 0)
        return 0;
    else
        foreach ($r as $row) { // = mysql_fetch_object($r))
            if ($row->catid == $val)
                return 1;
        }

    return 0;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_post_blog() {

    $arrImages = & get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID());

    if ($arrImages) {
        $arrKeys = array_keys($arrImages);
        $iNum = $arrKeys[0];
        $sThumbUrl = wp_get_attachment_thumb_url($iNum);
        $sImgString = '<a href="' . get_permalink() . '">' .
                '<img class="image_class" src="' . $sThumbUrl . '" width="100" height="100" />' .
                '</a>';
    } else {
        $sImgString = '<a href="' . get_permalink() . '">' .
                '<img class="image_class" src="' . get_bloginfo('template_url') . '/images/nopic.png" width="100" height="100" />' .
                '</a>';
    }
    ?>
    <div class="post vc_POST" id="post-<?php the_ID(); ?>">
        <div class="padd10">
            <div class="image_holder" style="width:120px">
    <?php echo $sImgString; ?>
            </div>
            <div  class="title_holder" style="width:510px" > 
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                <?php the_title(); ?></a></h2>
                <p class="mypostedon"><?php _e('Posted on', 'AuctionTheme'); ?> <?php the_time('F jS, Y') ?>  <?php _e('by', 'AuctionTheme'); ?> 
    <?php the_author() ?>
                </p>
                <p class="blog_post_preview"> <?php the_excerpt(); ?></p>


                <a href="<?php the_permalink() ?>" class="post_bid_btn"><?php _e('Read More', 'AuctionTheme'); ?></a>

            </div></div>



    </div>
    <?php
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function auctionTheme_small_post($param = '') {
    $ending = get_post_meta(get_the_ID(), 'ending', true);
    $sec = $ending - current_time('timestamp', 0);
    $location = get_post_meta(get_the_ID(), 'Location', true);


    $price = get_post_meta(get_the_ID(), 'price', true);
    $closed = get_post_meta(get_the_ID(), 'closed', true);
    $views = get_post_meta(get_the_ID(), 'views', true);
    $rnd = rand(1, 999);
    ?>
    <div class="post small-padd-top" >
        <div class="image_holder2">
            <a href="<?php the_permalink(); ?>"><img width="50" alt="<?php the_title(); ?>" height="50" class="image_class" 
                                                     src="<?php echo AuctionTheme_get_first_post_image(get_the_ID(), 75, 65); ?>" /></a>
        </div>
        <div  class="title_holder2" > 
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>

            <p class="mypostedon2">
    <?php _e("Posted in", 'AuctionTheme'); ?> <?php echo get_the_term_list(get_the_ID(), 'auction_cat', '', ', ', ''); ?><br/>
                    <?php _e("Location", 'AuctionTheme'); ?>: <?php
                    $lc = get_the_term_list(get_the_ID(), 'auction_location', '', ', ', '');
                    echo (empty($lc) ? __("not defined", 'AuctionTheme') : $lc );
                    ?><br/>
                <?php
                if ($param == 'view'):
                    ?>

                    <?php _e("Views", 'AuctionTheme'); ?>: <?php echo $views; ?>

                <?php else: ?>

                    <?php _e("Ending in", 'AuctionTheme'); ?>: <?php echo AuctionTheme_prepare_seconds_to_words($sec); ?>

                <?php endif; ?> 
            </p>




        </div></div> <?php
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function AuctionTheme_get_CATID($slug) {
                $c = get_term_by('slug', $slug, 'auction_cat');
                return $c->term_id;
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function AuctionTheme_clear_table($colspan = '') {
                return '<tr><td colspan="' . $colspan . '">&nbsp;</td></tr>';
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function auctionTheme_pay_for_item_link($bid_id) {
                //AuctionTheme_my_account_pay_for_item_id	

                $using_perm = AuctionTheme_using_permalinks();
                if ($using_perm)
                    return get_permalink(get_option('AuctionTheme_my_account_pay_for_item_id')) . "?bid_id=" . $bid_id;
                else
                    return get_bloginfo('siteurl') . "/?page_id=" . get_option('AuctionTheme_my_account_pay_for_item_id') . "&bid_id=" . $bid_id;
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function auctionTheme_pay_for_item_by_credits_link($bid_id) {
                //AuctionTheme_my_account_pay_for_item_id	

                $using_perm = AuctionTheme_using_permalinks();
                if ($using_perm)
                    return get_permalink(get_option('AuctionTheme_my_account_pay_item_cr_id')) . "?bid_id=" . $bid_id;
                else
                    return get_bloginfo('siteurl') . "/?page_id=" . get_option('AuctionTheme_my_account_pay_item_cr_id') . "&bid_id=" . $bid_id;
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function auctionTheme_add_theme_styles() {
                global $wp_query;
                $new_auction_step = $wp_query->query_vars['step'];
                $a_action = $wp_query->query_vars['a_action'];

                wp_register_style('bx_styles', get_bloginfo('template_url') . '/css/bx_styles.css', array(), '20120822', 'all');
                wp_register_script('social_pr', get_bloginfo('template_url') . '/js/connect.js');

                wp_register_script('easing', get_bloginfo('template_url') . '/js/jquery.easing.1.3.js');
                wp_register_script('bx_slider', get_bloginfo('template_url') . '/js/jquery.bxSlider.min.js');
                wp_register_script('jquery_cowntdown', get_bloginfo('template_url') . '/js/jquery.countdown.js');


                wp_register_style('bootstrap_style1', get_bloginfo('template_url') . '/css/bootstrap_min.css', array(), '20120822', 'all');
                wp_register_style('bootstrap_style2', get_bloginfo('template_url') . '/css/css.css', array(), '20120822', 'all');
                wp_register_style('bootstrap_style3', get_bloginfo('template_url') . '/css/bootstrap_responsive.css', array(), '20120822', 'all');
                wp_register_style('bootstrap_ie6', get_bloginfo('template_url') . '/css/bootstrap_ie6.css', array(), '20120822', 'all');
                wp_register_style('bootstrap_gal', get_bloginfo('template_url') . '/css/bootstrap_gal.css', array(), '20120822', 'all');
                wp_register_style('fileupload_ui', get_bloginfo('template_url') . '/css/fileupload_ui.css', array(), '20120822', 'all');
                wp_register_style('uploadify_css', get_bloginfo('template_url') . '/lib/uploadify/uploadify.css', array(), '20120822', 'all');


                wp_register_script('html5_js', get_bloginfo('template_url') . '/js/html5.js');
                wp_register_script('jquery_ui', get_bloginfo('template_url') . '/js/vendor/jquery.ui.widget.js');
                wp_register_script('templ_min', get_bloginfo('template_url') . '/js/templ.min.js');
                wp_register_script('load_image', get_bloginfo('template_url') . '/js/load_image.min.js');
                wp_register_script('canvas_to_blob', get_bloginfo('template_url') . '/js/canvas_to_blob.js');
                wp_register_script('iframe_transport', get_bloginfo('template_url') . '/js/jquery.iframe-transport.js');


                wp_register_style('fileupload_ui', get_bloginfo('template_url') . '/css/fileupload_ui.css', array(), '20120822', 'all');
                wp_register_script('fileupload_main', get_bloginfo('template_url') . '/js/jquery.fileupload.js');
                wp_register_script('fileupload_fp', get_bloginfo('template_url') . '/js/jquery.fileupload-fp.js');
                wp_register_script('fileupload_ui', get_bloginfo('template_url') . '/js/jquery.fileupload-ui.js');

                wp_register_script('locale_thing', get_bloginfo('template_url') . '/js/locale.js');

                wp_register_script('main_thing', get_bloginfo('template_url') . '/js/main.js');

                global $wp_styles, $wp_scripts;
                // enqueing:

                wp_enqueue_style('bx_styles');
                wp_enqueue_script('social_pr');
                wp_enqueue_script('easing');
                wp_enqueue_script('bx_slider');
                wp_enqueue_script('jquery_cowntdown');


                if ($new_auction_step == "2" or $a_action == "edit_auction" or $a_action == "repost_auction"):

                    // enqueing:
                    wp_enqueue_style('bootstrap_style1');
                    wp_enqueue_style('bootstrap_style2');
                    wp_enqueue_style('bootstrap_style3');
                    wp_enqueue_style('bootstrap_ie6');
                    wp_enqueue_style('bootstrap_gal');
                    wp_enqueue_style('fileupload_ui');
                    wp_enqueue_style('uploadify_css');

                    wp_enqueue_script('html5_js');
                    wp_enqueue_script('jquery_ui');
                    wp_enqueue_script('templ_min');
                    wp_enqueue_script('load_image');
                    wp_enqueue_script('canvas_to_blob');
                    wp_enqueue_script('iframe_transport');

                    wp_enqueue_script('fileupload_main');
                    wp_enqueue_script('fileupload_fp');
                    wp_enqueue_script('fileupload_ui');
                    wp_enqueue_script('locale_thing');
                    wp_enqueue_script('main_thing');
                    wp_enqueue_script('uploadify_js');

                //$wp_styles->add_data('bootstrap_ie6', 'conditional', 'lte IE 7');

                endif;
            }

            /*             * ***********************************************************
             *
             * 	AuctionTheme (c) sitemile.com - function
             *
             * ************************************************************ */

            function AuctionTheme_add_max_nr_of_images() {
                ?>

    <script type="text/javascript">
    <?php
    $AuctionTheme_enable_max_images_limit = get_option('AuctionTheme_enable_max_images_limit');
    if ($AuctionTheme_enable_max_images_limit == "yes") {
        $auctionTheme_nr_max_of_images = get_option('auctionTheme_nr_max_of_images');
        if (empty($auctionTheme_nr_max_of_images))
            $auctionTheme_nr_max_of_images = 10;
    }
    else
        $AuctionTheme_enable_max_images_limit = 1000;

    if (empty($auctionTheme_nr_max_of_images))
        $auctionTheme_nr_max_of_images = 100;
    ?>
    		
    		
    		
                    var maxNrImages_PT = <?php echo $auctionTheme_nr_max_of_images; ?>;
    	
    </script>

    <?php
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_categories($taxo, $selected = "", $include_empty_option = "", $ccc = "") {
    $args = "orderby=name&order=ASC&hide_empty=0&parent=0";
    $terms = get_terms($taxo, $args);

    $ret = '<select name="' . $taxo . '_cat" class="' . $ccc . '" id="' . $ccc . '">';
    if (!empty($include_empty_option))
        $ret .= "<option value=''>" . $include_empty_option . "</option>";

    if (empty($selected))
        $selected = -1;

    foreach ($terms as $term) {
        $id = $term->term_id;

        $ret .= '<option ' . ($selected == $id ? "selected='selected'" : " " ) . ' value="' . $id . '">' . $term->name . '</option>';

        $args = "orderby=name&order=ASC&hide_empty=0&parent=" . $id;
        $sub_terms = get_terms($taxo, $args);

        if ($sub_terms)
            foreach ($sub_terms as $sub_term) {
                $sub_id = $sub_term->term_id;
                $ret .= '<option ' . ($selected == $sub_id ? "selected='selected'" : " " ) . ' value="' . $sub_id . '">&nbsp; &nbsp;|&nbsp;  ' . $sub_term->name . '</option>';

                $args2 = "orderby=name&order=ASC&hide_empty=0&parent=" . $sub_id;
                $sub_terms2 = get_terms($taxo, $args2);

                if ($sub_terms2)
                    foreach ($sub_terms2 as $sub_term2) {
                        $sub_id2 = $sub_term2->term_id;
                        $ret .= '<option ' . ($selected == $sub_id2 ? "selected='selected'" : " " ) . ' value="' . $sub_id2 . '">&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; 
				 ' . $sub_term2->name . '</option>';

                        $args3 = "orderby=name&order=ASC&hide_empty=0&parent=" . $sub_id2;
                        $sub_terms3 = get_terms($taxo, $args3);

                        if ($sub_terms3)
                            foreach ($sub_terms3 as $sub_term3) {
                                $sub_id3 = $sub_term3->term_id;
                                $ret .= '<option ' . ($selected == $sub_id3 ? "selected='selected'" : " " ) . ' value="' . $sub_id3 . '">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; 
					 ' . $sub_term3->name . '</option>';
                            }
                    }
            }
    }

    $ret .= '</select>';

    return $ret;
}

function AuctionTheme_send_email($recipients, $subject = '', $message = '') {

    $AuctionTheme_email_addr_from = get_option('AuctionTheme_email_addr_from');
    $AuctionTheme_email_name_from = get_option('AuctionTheme_email_name_from');

    $message = stripslashes($message);
    $subject = stripslashes($subject);

    if (empty($AuctionTheme_email_name_from))
        $AuctionTheme_email_name_from = "Auction Theme";
    if (empty($AuctionTheme_email_addr_from))
        $AuctionTheme_email_addr_from = "AuctionTheme@wordpress.org";

    $headers = 'From: ' . $AuctionTheme_email_name_from . ' <' . $AuctionTheme_email_addr_from . '>' . PHP_EOL;
    $AuctionTheme_allow_html_emails = get_option('AuctionTheme_allow_html_emails');
    if ($AuctionTheme_allow_html_emails != "yes")
        $html = false;
    else
        $html = true;

    $ok_send_email = true;
    $ok_send_email = apply_filters('AuctionTheme_ok_to_send_emails', $ok_send_email);

    if ($ok_send_email == true) {
        if ($html) {
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-Type: " . get_bloginfo('html_type') . "; charset=\"" . get_bloginfo('charset') . "\"\n";
            $mailtext = "<html><head><title>" . $subject . "</title></head><body>" . nl2br($message) . "</body></html>";
            return wp_mail($recipients, $subject, $mailtext, $headers);
        } else {
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-Type: text/plain; charset=\"" . get_bloginfo('charset') . "\"\n";
            $message = preg_replace('|&[^a][^m][^p].{0,3};|', '', $message);
            $message = preg_replace('|&amp;|', '&', $message);
            $mailtext = wordwrap(strip_tags($message), 80, "\n");
            return wp_mail($recipients, stripslashes($subject), stripslashes($mailtext), $headers);
        }
    }
}

function auctionTheme_get_highest_bid_owner($pid) {
    global $wpdb;
    $s = "select bid from " . $wpdb->prefix . "auction_bids where pid='$pid' order by bid desc limit 1";
    $r = $wpdb->get_results($s);

    if (count($r) == 0)
        return false;

    $r = $r[0];
    return $r->uid;
}

function auctionTheme_save_custom_fields($pid) {

    if (isset($_POST['fromadmin'])) {

        $now = current_time('timestamp', 0);
        $ending = get_post_meta($pid, "ending", true);
        $views = get_post_meta($pid, "views", true);
        $closed = get_post_meta($pid, "closed", true);
        $reverse = get_post_meta($pid, "reverse", true);

        update_post_meta($pid, "quant", trim($_POST['quant']));
        update_post_meta($pid, "ending", strtotime($_POST['ending'], $now));

        if (empty($views))
            update_post_meta($pid, "views", 0);


        if ($reverse == "yes")
            update_post_meta($pid, "reverse", "yes");
        else
            update_post_meta($pid, "reverse", "no");


        $reverse = get_post_meta($pid, "reverse", true);

        update_post_meta($pid, "only_buy_now", (isset($_POST['only_buy_now']) ? "1" : "0"));

        if ($_POST['featureds'] == '1')
            update_post_meta($pid, "featured", '1');
        else
            update_post_meta($pid, "featured", '0');

        if ($_POST['closed'] == '1') {

            update_post_meta($pid, "closed", '1');
        } else {
            if ($closed == "1")
                update_post_meta($pid, "ending", current_time('timestamp', 0) + 30 * 24 * 3600);
            update_post_meta($pid, "closed", '0');
        }

        if (isset($_POST['shipping']))
            update_post_meta($pid, "shipping", auctionTheme_clear_sums_of_cash($_POST['shipping']));

        if (isset($_POST['start_price']))
            update_post_meta($pid, "start_price", auctionTheme_clear_sums_of_cash($_POST['start_price']));

        if (isset($_POST['reserve']))
            update_post_meta($pid, "reserve", auctionTheme_clear_sums_of_cash($_POST['reserve']));

        if (isset($_POST['buy_now']))
            update_post_meta($pid, "buy_now", auctionTheme_clear_sums_of_cash($_POST['buy_now']));

        if (isset($_POST['private_bids']))
            update_post_meta($pid, "private_bids", $_POST['private_bids']);


        if (isset($_POST['price']))
            update_post_meta($pid, "price", auctionTheme_clear_sums_of_cash($_POST['price']));

        if (isset($_POST['Location']))
            update_post_meta($pid, "Location", $_POST['Location']);

        //----------------------

        for ($i = 0; $i < count($_POST['custom_field_id']); $i++) {
            $id = $_POST['custom_field_id'][$i];
            $valval = $_POST['custom_field_value_' . $id];

            if (is_array($valval))
                update_post_meta($pid, 'custom_field_ID_' . $id, $valval);
            else
                update_post_meta($pid, 'custom_field_ID_' . $id, strip_tags($valval));
        }
    }

    update_post_meta($pid, 'unpaid', '0');


    $ggcbd = get_post_meta($pid, "current_bid", true);

    if (empty($ggcbd)) {
        if ($reverse == "yes") {
            update_post_meta($pid, "current_bid", $_POST['price']);
        } else {
            update_post_meta($pid, "current_bid", $_POST['start_price']);
            if (empty($_POST['start_price'])) {
                if (!empty($_POST['buy_now']))
                    update_post_meta($pid, "current_bid", $_POST['buy_now']);
                else
                    update_post_meta($pid, "current_bid", 0);
            }
        }
    }
    //-----------------------------------

    $zip = get_post_meta($pid, "Location", true);

    $loc = wp_get_post_terms($pid, 'auction_location');
    $loc_a = '';

    foreach ($loc as $l)
        $loc_a .= $l->name . ',';

    $loc_a .= $zip;

    $data = AuctionTheme_get_geo_coordinates($loc_a);
    $long = $data[3];
    $lat = $data[2];

    update_post_meta($pid, 'auction_lat', $lat);
    update_post_meta($pid, 'auction_long', $long);
}

function auctionTheme_my_auctions_columns($columns) { //this function display the columns headings
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => __("Auction Title", "AuctionTheme"),
        "author" => __("Author", "AuctionTheme"),
        "posted" => __("Posted On", "AuctionTheme"),
        "price" => __("Price", "AuctionTheme"),
        "exp" => __("Expires in", "AuctionTheme"),
        "feat" => __("Featured", "AuctionTheme"),
        "thumbnail" => __("Thumbnail", "AuctionTheme"),
        "options" => __("Options", "AuctionTheme")
    );
    return $columns;
}

function auctionTheme_my_custom_columns($column) {
    global $post;
    if ("ID" == $column)
        echo $post->ID; //displays title
    elseif ("description" == $column)
        echo $post->ID; //displays the content excerpt
    elseif ("posted" == $column)
        echo date('jS \of F, Y \<\b\r\/\>H:i:s', strtotime($post->post_date)); //displays the content excerpt
    elseif ("thumbnail" == $column) {
        echo '<a href="' . get_bloginfo('siteurl') . '/wp-admin/post.php?post=' . $post->ID . '&action=edit"><img class="image_class" 
	src="' . AuctionTheme_get_first_post_image($post->ID, 75, 65) . '" width="75" height="65" /></a>'; //shows up our post thumbnail that we previously created.
    } elseif ("author" == $column) {
        echo $post->post_author;
    } elseif ("feat" == $column) {
        $f = get_post_meta($post->ID, 'featured', true);
        if ($f == "1")
            echo __("Yes", "AuctionTheme");
        else
            echo __("No", "AuctionTheme");
    }

    elseif ("price" == $column) {
        echo auctionTheme_get_show_price(auctionTheme_get_current_price($post->ID));
    } elseif ("exp" == $column) {
        $ending = get_post_meta($post->ID, 'ending', true);
        echo AuctionTheme_prepare_seconds_to_words($ending - current_time('timestamp', 0));
    } elseif ("options" == $column) {
        echo '<div style="padding-top:20px">';
        echo '<a class="awesome" href="' . get_bloginfo('siteurl') . '/wp-admin/post.php?post=' . $post->ID . '&action=edit">Edit</a> ';
        echo '<a class="awesome" href="' . get_permalink($post->ID) . '" target="_blank">View</a> ';
        echo '<a class="trash" href="' . get_delete_post_link($post->ID) . '">Trash</a> ';
        echo '</div>';
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_login_url() {
    return get_bloginfo('siteurl') . "/wp-login.php";
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_add_query_vars($public_query_vars) {
    $public_query_vars[] = 'jb_action';
    $public_query_vars[] = 'a_action';
    $public_query_vars[] = 'orderid';
    $public_query_vars[] = 'bid_id';

    $public_query_vars[] = 'step';
    $public_query_vars[] = 'my_second_page';
    $public_query_vars[] = 'third_page';
    $public_query_vars[] = 'username';
    $public_query_vars[] = 'pid';
    $public_query_vars[] = 'term_search';  //job_sort, job_category, page
    $public_query_vars[] = 'method';
    $public_query_vars[] = 'post_author';
    $public_query_vars[] = 'page';
    $public_query_vars[] = 'rid';

    return $public_query_vars;
}

function auctionTheme_get_winner_bid($bid_id) {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
    $r = $wpdb->get_results($s);
    return $r[0];
}

function auctionTheme_prepare_rating($pid, $fromuser, $touser, $winner_bid) {

    global $wpdb;
    $s = "insert into " . $wpdb->prefix . "auction_ratings (pid, fromuser, touser, bid_id) values('$pid','$fromuser','$touser', '$winner_bid')";
    $wpdb->query($s);

    AuctionTheme_send_email_when_review_needs_to_be_awarded($pid, $fromuser, $touser);
    AuctionTheme_send_email_when_review_needs_to_be_awarded($pid, $touser, $fromuser);

    //mysql_query($s) or die(mysql_error());
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_auto_draft($uid) {
    global $wpdb;
    $querystr = "
		SELECT distinct wposts.* 
		FROM $wpdb->posts wposts where 
		wposts.post_author = '$uid' AND wposts.post_status = 'auto-draft' 
		AND wposts.post_type = 'auction' 
		ORDER BY wposts.ID DESC LIMIT 1 ";

    $row = $wpdb->get_results($querystr, OBJECT);
    if (count($row) > 0) {
        $row = $row[0];
        return $row->ID;
    }

    return AuctionTheme_create_auto_draft($uid);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_create_auto_draft($uid) {
    $my_post = array();
    $my_post['post_title'] = 'Auto Draft';
    $my_post['post_type'] = 'auction';
    $my_post['post_status'] = 'auto-draft';
    $my_post['post_author'] = $uid;
    $pid = wp_insert_post($my_post, true);

    update_post_meta($pid, 'base_fee_paid', "0");
    update_post_meta($pid, 'featured_paid', "0");
    update_post_meta($pid, 'private_bids_paid', "0");

    return $pid;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_using_permalinks() {
    global $wp_rewrite;
    if ($wp_rewrite->using_permalinks())
        return true;
    else
        return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_post_new_with_pid_stuff_thg($pid, $step = 1, $fin = '') {
    $using_perm = AuctionTheme_using_permalinks();
    if ($using_perm)
        return get_permalink(get_option('AuctionTheme_post_new_page_id')) . "?auction_id=" . $pid . "&step=" . $step . (!empty($fin) ? "&finalise=yes" : '');
    else
        return get_bloginfo('siteurl') . "/?page_id=" . get_option('AuctionTheme_post_new_page_id') . "&auction_id=" . $pid . "&step=" . $step . (!empty($fin) ? "&finalise=yes" : '');
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_clear_sums_of_cash($cash) {
    $cash = str_replace(" ", "", $cash);
    $cash = str_replace(",", "", $cash);
    //$cash = str_replace(".","",$cash);

    return strip_tags($cash);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_buy_it_now_price($pid = '') {

    if (empty($pid))
        $pid = get_the_ID();
    $price = get_post_meta($pid, 'buy_now', true);

    if (empty($price))
        $price = false;
    return $price;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function get_auction_fields_values($pid) {
    $cat = wp_get_object_terms($pid, 'auction_cat');

    $catid = $cat[0]->term_id;

    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_custom_fields order by ordr asc "; //where cate='all' OR cate like '%|$catid|%' order by ordr asc";	
    $r = $wpdb->get_results($s);



    $arr = array();
    $i = 0;

    foreach ($r as $row) { // = mysql_fetch_object($r))

        $pmeta = get_post_meta($pid, "custom_field_ID_" . $row->id);

        if (!empty($pmeta) && count($pmeta) > 0) {
            $arr[$i]['field_name'] = $row->name;

            if (!empty($pmeta)) {
                $arr[$i]['field_name'] = $row->name;
                $arr[$i]['field_value'] = $pmeta;
                $i++;
            }
        }
    }

    return $arr;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_auction_stars($rating) {
    $full = get_bloginfo('template_url') . "/images/full_star.gif";
    $empty = get_bloginfo('template_url') . "/images/empty_star.gif";

    $r = '';

    for ($j = 1; $j <= $rating; $j++)
        $r .= '<img src="' . $full . '" alt="star" />';


    for ($j = 5; $j > $rating; $j--)
        $r .= '<img src="' . $empty . '" alt="star" />';

    return $r;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auction_get_star_rating($uid) {

    global $wpdb;
    $s = "select grade from " . $wpdb->prefix . "auction_ratings where touser='$uid' AND awarded='1'";
    $r = $wpdb->get_results($s);
    $i = 0;
    $s = 0;

    if (count($r) == 0)
        return __('(no rating)', 'AuctionTheme');
    else
        foreach ($r as $row) { // = mysql_fetch_object($r))
            $i++;
            $s = $s + $row->grade;
        }

    $rating = round($s / $i, 0);

    return AuctionTheme_get_auction_stars(round($rating / 2));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_post_images($pid, $limit = -1) {

    //---------------------
    // build the exclude list
    $exclude = array();

    $args = array(
        'order' => 'ASC',
        'post_type' => 'attachment',
        'post_parent' => get_the_ID(),
        'meta_key' => 'another_reserved1',
        'meta_value' => '1',
        'numberposts' => -1,
        'post_status' => null,
    );
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $url = $attachment->ID;
            array_push($exclude, $url);
        }
    }

    //-----------------


    $arr = array();

    $args = array(
        'order' => 'ASC',
        'orderby' => 'post_date',
        'post_type' => 'attachment',
        'post_parent' => $pid,
        'exclude' => $exclude,
        'post_mime_type' => 'image',
        'numberposts' => $limit,
    );
    $i = 0;

    $attachments = get_posts($args);
    if ($attachments) {

        foreach ($attachments as $attachment) {

            $url = wp_get_attachment_url($attachment->ID);
            array_push($arr, $url);
        }
        return $arr;
    }
    return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_is_owner_of_post() {

    if (!is_user_logged_in())
        return false;

    global $current_user;
    get_currentuserinfo();

    $post = get_post(get_the_ID());
    if ($post->post_author == $current_user->ID)
        return true;
    return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_userid_from_username($user) {
    $user = get_user_by('login', $user);
    if ($user == false)
        return false;

    return $user->ID;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_user_profile_link($uid) {
    return get_bloginfo('siteurl') . '/?a_action=user_profile&post_author=' . $uid;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_user_feedback_link($uid) {
    return get_bloginfo('siteurl') . '/?a_action=user_feedback&post_author=' . $uid;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_update_credits($uid, $am) {

    update_user_meta($uid, 'credits', $am);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auction_get_my_awarded_auctions($uid) {
    $c = "<select name='auctionss'>";
    global $wpdb;

    $querystr = "
					SELECT distinct wposts.* 
					FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, " . $wpdb->prefix . "term_relationships rels 
					WHERE wposts.post_author='$uid' 
					AND  wposts.ID = wpostmeta.post_id 
					AND wpostmeta.meta_key = 'closed' 
					AND wpostmeta.meta_value = '1' 
					AND wposts.post_status = 'publish' 
					AND wposts.post_type = 'auction' AND rels.object_id=wposts.ID AND rels.term_taxonomy_id!='$blgid'  
					ORDER BY wposts.post_date DESC";

    //echo $querystr;
    $r = $wpdb->get_results($querystr);

    foreach ($r as $row) {
        $pid = $row->ID;
        $winner = get_post_meta($pid, "winner", true);
        if (!empty($winner)) {
            $c .= '<option value="' . $row->ID . '">' . $row->post_title . '</option>';
            $i = 1;
        }
    }

    //----------------------------

    $querystr = "
					SELECT distinct wposts.* 
					FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
					WHERE wposts.ID = wpostmeta.post_id 
					AND wpostmeta.meta_key = 'winner' 
					AND wpostmeta.meta_value = '$uid' 
					AND wposts.post_status = 'publish' 
					AND wposts.post_type = 'auction'  
					ORDER BY wposts.post_date DESC ";



    $r = $wpdb->get_results($querystr);

    foreach ($r as $row) {
        $pid = $row->ID;

        $c .= '<option value="' . $row->ID . '">' . $row->post_title . '</option>';
        $i = 1;
    }

    //-------------------------------

    if ($i == 1)
        return $c . '</select>';

    return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_insert_pic_media_lib($author, $pid, $uri, $path, $post_title, $another_reserved1 = '') {
    require_once(ABSPATH . '/wp-admin/includes/image.php');
    $wp_filetype = wp_check_filetype(basename($path), null);

    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_author' => $author,
        'guid' => $uri,
        'post_parent' => $pid,
        'post_type' => 'attachment',
        'post_title' => $post_title
    );

    $id = wp_insert_attachment($attachment, $path, $pid);

    if (!empty($another_reserved1)) {
        update_post_meta($id, 'another_reserved1', '1');
    }

    $dt = wp_generate_attachment_metadata($id, $path);
    wp_update_attachment_metadata($id, $dt);
    return $id;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_current_user_has_bid($uid, $res) {
    foreach ($res as $row)
        if ($row->uid == $uid) {
            return true;
        }

    return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_curPageURL_me() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_sm_replace_me($s) {
    return urlencode($s);
}

function auctionTheme_template_redirect() {

    if (isset($_POST['deposit_pay_me'])) {
        global $am_err;
        $amount = trim($_POST['amount']);
        if (empty($amount))
            $am_err = 1;
        elseif (!is_numeric($amount))
            $am_err = 1;
        else {
            wp_redirect(get_bloginfo('siteurl') . "/?a_action=deposit_pay&am=" . $amount);
            exit;
        }
    }

    global $wp;
    global $wp_query, $wp_rewrite, $post;
    $paagee = $wp_query->query_vars['my_custom_page_type'];
    $a_action = $wp_query->query_vars['a_action'];

    $my_pid = $post->ID;
    $AuctionTheme_post_new_page_id = get_option('AuctionTheme_post_new_page_id');
    $AuctionTheme_my_account_page_id = get_option('AuctionTheme_my_account_page_id');

    if (isset($_GET['test'])) {
        include 'test.php';
        die();
    }

    if ($post->post_parent == $AuctionTheme_my_account_page_id) {
        if (!is_user_logged_in()) {
            wp_redirect(get_bloginfo('siteurl') . "/wp-login.php?redirect_to=" . AuctionTheme_sm_replace_me(AuctionTheme_curPageURL_me()));
            exit;
        }
    }

    if (isset($_GET['notify_chained'])) {

        if ($_POST['status'] == "COMPLETED") {
            $trID = $_POST['tracking_id'];
            $trID = explode("_", $trID);
            $bid_id = $trID[0];

            global $wpdb;

            $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
            $r = $wpdb->get_results($s);
            $row = $r[0];
            $bid = $row;

            AuctionTheme_send_email_when_item_is_paid_seller($bid->pid, $bid_id);
            AuctionTheme_send_email_when_item_is_paid_buyer($bid->pid, $bid_id);

            $wpdb->query("update " . $wpdb->prefix . "auction_bids set paid='1' where id='$bid_id'");
            update_post_meta($pid, 'paid_on_' . $bid_id, current_time('timestamp', 0));
        }

        exit;
    }

    if (isset($_GET['return_chained'])) {

        $ret_id = $_GET['return_chained'];

        $using_perm = AuctionTheme_using_permalinks();
        $paid_items_id = get_option('AuctionTheme_my_account_paid_id');

        if ($using_perm)
            $paid_itms_m = get_permalink($paid_items_id) . "?";
        else
            $paid_itms_m = get_bloginfo('siteurl') . "/?page_id=" . $paid_items_id . "&";

        wp_redirect($paid_itms_m . "paid_ok=1");
    }


    if ($my_pid == $AuctionTheme_post_new_page_id) {
        if (!is_user_logged_in()) {
            wp_redirect(AuctionTheme_login_url());
            exit;
        }

        if (!isset($_GET['auction_id']))
            $set_ad = 1; else
            $set_ad = 0;
        global $current_user;
        get_currentuserinfo();

        if ($set_ad == 1) {
            $pid = AuctionTheme_get_auto_draft($current_user->ID);
            wp_redirect(AuctionTheme_post_new_with_pid_stuff_thg($pid));
        }

        include 'lib/post_new_post.php';
    }

    if ($a_action == 'buy_now') {
        include 'lib/buy_now.php';
        die();
    }

    if ($a_action == 'user_profile') {
        include 'lib/user_profile.php';
        die();
    }

    if ($a_action == 'user_feedback') {
        include 'lib/user_feedback.php';
        die();
    }

    if ($a_action == 'deposit_pay') {
        include 'lib/gateways/deposit_pay.php';
        die();
    }

    if ($a_action == 'relist_auction') {
        include 'lib/my_account/relist_auction.php';
        die();
    }



    if ($a_action == 'rate_user') {
        include 'lib/my_account/rate_user.php';
        die();
    }


    if ($a_action == 'delete_auction') {
        include 'lib/my_account/delete_auction.php';
        die();
    }

    if ($a_action == 'mark_shipped') {
        include 'lib/my_account/mark_shipped.php';
        die();
    }


    if (isset($_GET['pay_for_item'])) {
        include 'lib/gateways/pay_for_item_' . $_GET['pay_for_item'] . '.php';
        die();
    }

    if ($a_action == "mb_listing") {
        include 'lib/gateways/moneybookers_listing.php';
        die();
    }

    if ($a_action == "paypal_listing") {
        include 'lib/gateways/paypal_listing.php';
        die();
    }

    if ($a_action == "credits_listing") {
        include 'lib/gateways/credits_listing.php';
        die();
    }

    if ($a_action == "edit_auction") {
        include 'lib/my_account/edit_auction.php';
        die();
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_formats_special($number, $cents = 1) { // cents: 0=never, 1=if needed, 2=always
    $dec_sep = '.';
    $tho_sep = ',';

    //dec,thou

    if (is_numeric($number)) { // a number
        if (!$number) { // zero
            $money = ($cents == 2 ? '0' . $dec_sep . '00' : '0'); // output zero
        } else { // value
            if (floor($number) == $number) { // whole number
                $money = number_format($number, ($cents == 2 ? 2 : 0), $dec_sep, ''); // format
            } else { // cents
                $money = number_format(round($number, 2), ($cents == 0 ? 0 : 2), $dec_sep, ''); // format
            } // integer or decimal
        } // value
        return $money;
    } // numeric
}

// formatMoney

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_active_auctions_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_active_act\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_active_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_active_act\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_pay_4_item_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_pay4item\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_pay4item_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_pay4item\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_all_locations_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_show_all_locations\]/", $content)) {
        ob_start();
        AuctionTheme_all_locations_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_show_all_locations\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_blog_page_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_blog_posts\]/", $content)) {
        ob_start();
        AuctionTheme_display_blog_page_disp();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_blog_posts\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_all_categories_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_show_all_categories\]/", $content)) {
        ob_start();
        AuctionTheme_all_categories_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_show_all_categories\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_closed_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_closed_act\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_closed_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_closed_act\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_unpub_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_unpub_act\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_unpub_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_unpub_act\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_adv_search_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_adv_search\]/", $content)) {
        ob_start();
        AuctionTheme_adv_search_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_adv_search\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_won_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_won_items\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_won_items_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_won_items\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

function AuctionTheme_my_account_paid__items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_paid_\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_paid__area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_paid_\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_paid_n_shipped_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_paid_ship\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_paid_n_shipped_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_paid_ship\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_outstanding_payment_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_outstanding_payments\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_outstanding_payments_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_outstanding_payments\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_I_bid_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_bid_items\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_items_i_bid_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_bid_items\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_NO_won_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_not_won_items\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_not_won_items_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_not_won_items\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_pay_item_by_credits_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_pay_item_by_credits\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_pay_item_by_credits_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_pay_item_by_credits\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_shipped_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_shipped\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_shipped_items_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_shipped\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_not_shipped_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_no_shipped\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_not_shipped_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_no_shipped\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_aw_pay_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_aw_pay\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_awaiting_payment_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_aw_pay\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_sold_items_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_sold_act\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_sold_auctions_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_sold_act\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_reviews_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_reviews\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_reviews_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_reviews\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_payments_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_payments\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_payments_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_payments\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_priv_mess_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_priv_mess\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_priv_mess_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_priv_mess\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_pers_info_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account_personal_info\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_pers_info_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account_personal_info\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_my_account\]/", $content)) {
        ob_start();
        AuctionTheme_my_account_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_my_account\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

function AuctionTheme_watch_list_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_watch_list\]/", $content)) {
        ob_start();
        AuctionTheme_watch_list_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_watch_list\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_post_new_look_for_stuff($content = '') {
    if (preg_match("/\[auction_theme_post_new\]/", $content)) {
        ob_start();
        AuctionTheme_post_new_area_function();
        $output = ob_get_contents();
        ob_end_clean();
        $output = str_replace('$', '\$', $output);
        return preg_replace("/(<p>)*\[auction_theme_post_new\](<\/p>)*/", $output, $content);
    } else {
        return $content;
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_admin_main_head_scr() {
    ?>	

    <link rel="stylesheet" href="<?php echo get_bloginfo('template_url'); ?>/css/admin.css" type="text/css" />    
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/colorpicker.css" type="text/css" />
    <link rel="stylesheet" media="screen" type="text/css" href="<?php bloginfo('template_url'); ?>/css/layout.css" />
    <link type="text/css" href="<?php bloginfo('template_url'); ?>/css/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	

    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/idtabs.js"></script>	
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.ui.core.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery.ui.widget.js"></script>

    <script type="text/javascript">
    			
        var $ = jQuery;
        $(document).ready(function() {		
            $("#usual2 ul").idTabs("tabs1"); 
        });
    </script>


    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/colorpicker.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/eye.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/utils.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/layout.js?ver=1.0.2"></script>		

    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_rewrite_rules($wp_rewrite) {

    global $category_url_link, $location_url_link;
    $new_rules = array(
        $category_url_link . '/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?auction_cat=' . $wp_rewrite->preg_index(1) . "&feed=" . $wp_rewrite->preg_index(2),
        $category_url_link . '/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?auction_cat=' . $wp_rewrite->preg_index(1) . "&feed=" . $wp_rewrite->preg_index(2),
        $category_url_link . '/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?auction_cat=' . $wp_rewrite->preg_index(1) . "&paged=" . $wp_rewrite->preg_index(2),
        $category_url_link . '/([^/]+)/?$' => 'index.php?auction_cat=' . $wp_rewrite->preg_index(1)
    );

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_create_post_type() {

    global $auctions_url_thing, $auction_thing_list;

    $icn = get_bloginfo('template_url') . "/images/auctionicon.gif";
    register_post_type('auction', array(
        'labels' => array(
            'name' => __('Auctions', 'AuctionTheme'),
            'singular_name' => __('Auction', 'AuctionTheme'),
            'add_new' => __('Add New Auction', 'AuctionTheme'),
            'new_item' => __('New Auction', 'AuctionTheme'),
            'edit_item' => __('Edit Auction', 'AuctionTheme'),
            'add_new_item' => __('Add New Auction', 'AuctionTheme'),
            'search_items' => __('Search Auctions', 'AuctionTheme'),
        ),
        'public' => true,
        'menu_position' => 5,
        'register_meta_box_cb' => 'auctionTheme_set_metaboxes',
        'has_archive' => "auction-list",
        'rewrite' => array('slug' => $auctions_url_thing . "/%auction_cat%", 'with_front' => false),
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        '_builtin' => false,
        'menu_icon' => $icn,
        'publicly_queryable' => true,
        'hierarchical' => false
            )
    );

    global $category_url_link, $location_url_link;

    register_taxonomy('auction_cat', 'auction', array('hierarchical' => true, 'label' => __('Auction Categories', 'AuctionTheme'),
        'rewrite' => true
            )
    );
    register_taxonomy('auction_location', 'auction', array('hierarchical' => true, 'label' => __('Locations', 'AuctionTheme'),
        'rewrite' => array('slug' => $location_url_link, 'with_front' => false)
    ));
    add_post_type_support('auction', 'author');
    // add_post_type_support( 'auction', 'custom-fields' );
    register_taxonomy_for_object_type('post_tag', 'auction');

    flush_rewrite_rules();
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_set_metaboxes() {

    add_meta_box('auction_custom_fields', 'Auction Custom Fields', 'auctionTheme_custom_fields_html', 'auction', 'advanced', 'high');
    add_meta_box('auction_images', 'Auction Images', 'auctionTheme_theme_auction_images', 'auction', 'advanced', 'high');
    add_meta_box('auction_bids', 'Auction Bids', 'auctionTheme_theme_auction_bids', 'auction', 'advanced', 'high');
    add_meta_box('auction_dets', 'Auction Details', 'auctionTheme_theme_auction_dts', 'auction', 'side', 'high');
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_theme_auction_images() {

    global $current_user;
    get_currentuserinfo();
    $cid = $current_user->ID;

    global $post;
    $pid = $post->ID;
    ?>


    <script type="text/javascript" src="<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/jquery.uploadify-3.1.js"></script>     
    <link rel="stylesheet" href="<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/uploadify.css" type="text/css" />

    <script type="text/javascript">
    	
        function delete_this(id)
        {
            $.ajax({
                method: 'get',
                url : '<?php echo get_bloginfo('siteurl'); ?>/index.php/?_ad_delete_pid='+id,
                dataType : 'text',
                success: function (text) {   $('#image_ss'+id).remove();  }
            });
            //alert("a");
    	
        }

    	
    	
        $(function() {
    		
            $("#fileUpload3").uploadify({
                height        : 30,
                auto:			true,
                swf           : '<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/uploadify.swf',
                uploader      : '<?php echo get_bloginfo('template_url'); ?>/lib/uploadify/uploady.php',
                width         : 120,
                fileTypeExts  : '*.jpg;*.jpeg;*.gif;*.png',
                formData    : {'ID':<?php echo $pid; ?>,'author':<?php echo $cid; ?>},
                onUploadSuccess : function(file, data, response) {
    			
                    //alert(data);
                    var bar = data.split("|");
    			
                    $('#thumbnails').append('<div class="div_div" id="image_ss'+bar[1]+'" ><img width="70" class="image_class" height="70" src="' + bar[0] + '" /><a href="javascript: void(0)" onclick="delete_this('+ bar[1] +')"><img border="0" src="<?php echo get_bloginfo('template_url'); ?>/images/delete_icon.png" border="0" /></a></div>');
                }
    	
    			
    			
            });
    		
    		
        });
    	
    	
    </script>

    <style type="text/css">
        .div_div
        {
            margin-left:5px; float:left; 
            width:110px;margin-top:10px;
        }

    </style>

    <div id="fileUpload3">You have a problem with your javascript</div>
    <div id="thumbnails" style="overflow:hidden;margin-top:20px">

    <?php
    $args = array(
        'order' => 'ASC',
        'orderby' => 'post_date',
        'post_type' => 'attachment',
        'post_parent' => $post->ID,
        'post_mime_type' => 'image',
        'numberposts' => -1,
    );
    $i = 0;

    $attachments = get_posts($args);



    if ($attachments) {
        foreach ($attachments as $attachment) {
            $url = wp_get_attachment_url($attachment->ID);

            echo '<div class="div_div"  id="image_ss' . $attachment->ID . '"><img width="70" class="image_class" height="70" src="' .
            AuctionTheme_generate_thumb($url, 70, 70) . '" />
			<a href="javascript: void(0)" onclick="delete_this(\'' . $attachment->ID . '\')"><img border="0" src="' . get_bloginfo('template_url') . '/images/delete_icon.png" /></a>
			</div>';
        }
    }
    ?>

    </div>

    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_option_drop_down($arr, $name) {
    $opts = get_option($name);
    $r = '<select name="' . $name . '">';
    foreach ($arr as $key => $value) {
        $r .= '<option value="' . $key . '" ' . ($opts == $key ? ' selected="selected" ' : "" ) . '>' . $value . '</option>';
    }
    return $r . '</select>';
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_post_new_link() {
    return get_permalink(get_option('AuctionTheme_post_new_page_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_watch_list() {
    return get_permalink(get_option('AuctionTheme_watch_list_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_blog_link() {
    return get_permalink(get_option('AuctionTheme_blog_home_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_advanced_search_link() {
    return get_permalink(get_option('AuctionTheme_adv_search_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_my_account_link() {
    return get_permalink(get_option('AuctionTheme_my_account_page_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_payments_link() {
    return get_permalink(get_option('AuctionTheme_my_account_payments_page_id'));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_categories_slug($taxo, $selected = "", $include_empty_option = "", $ccc = "") {
    $args = "orderby=name&order=ASC&hide_empty=0&parent=0";
    $terms = get_terms($taxo, $args);

    $ret = '<select name="' . $taxo . '_cat" class="' . $ccc . '" id="' . $ccc . '">';
    if (!empty($include_empty_option)) {

        if ($include_empty_option == "1")
            $include_empty_option = "Select";
        $ret .= "<option value=''>" . $include_empty_option . "</option>";
    }

    if (empty($selected))
        $selected = -1;

    foreach ($terms as $term) {
        $id = $term->slug;
        $ide = $term->term_id;

        $ret .= '<option ' . ($selected == $id ? "selected='selected'" : " " ) . ' value="' . $id . '">' . $term->name . '</option>';

        $args = "orderby=name&order=ASC&hide_empty=0&parent=" . $ide;
        $sub_terms = get_terms($taxo, $args);

        if ($sub_terms)
            foreach ($sub_terms as $sub_term) {
                $sub_id = $sub_term->slug;
                $ret .= '<option ' . ($selected == $sub_id ? "selected='selected'" : " " ) . ' value="' . $sub_id . '">&nbsp; &nbsp;|&nbsp;  ' . $sub_term->name . '</option>';

                $args2 = "orderby=name&order=ASC&hide_empty=0&parent=" . $sub_term->term_id;
                $sub_terms2 = get_terms($taxo, $args2);

                if ($sub_terms2)
                    foreach ($sub_terms2 as $sub_term2) {
                        $sub_id2 = $sub_term2->slug;
                        $ret .= '<option ' . ($selected == $sub_id2 ? "selected='selected'" : " " ) . ' value="' . $sub_id2 . '">&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp;  
				' . $sub_term2->name . '</option>';

                        $args3 = "orderby=name&order=ASC&hide_empty=0&parent=" . $sub_term2->term_id;
                        $sub_terms3 = get_terms($taxo, $args3);

                        if ($sub_terms3)
                            foreach ($sub_terms3 as $sub_term3) {
                                $sub_id3 = $sub_term3->slug;
                                $ret .= '<option ' . ($selected == $sub_id3 ? "selected='selected'" : " " ) . ' value="' . $sub_id3 . '">&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp;  
					' . $sub_term3->name . '</option>';
                            }
                    }
            }
    }

    $ret .= '</select>';

    return $ret;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_insert_pages($page_ids, $page_title, $page_tag, $parent_pg = 0) {

    $opt = get_option($page_ids);
    if (!AuctionTheme_check_if_page_existed($opt)) {

        $post = array(
            'post_title' => $page_title,
            'post_content' => $page_tag,
            'post_status' => 'publish',
            'post_type' => 'page',
            'post_author' => 1,
            'ping_status' => 'closed',
            'post_parent' => $parent_pg);

        $post_id = wp_insert_post($post);

        update_post_meta($post_id, '_wp_page_template', 'auction-special-page-template.php');
        update_option($page_ids, $post_id);
    }
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_check_if_page_existed($pid) {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "posts where post_type='page' AND post_status='publish' AND ID='$pid'";
    $r = $wpdb->get_results($s);

    if (count($r) > 0)
        return true;
    return false;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */


add_filter('post_type_link', 'AuctionTheme_post_type_link_filter_function', 1, 3);

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_post_type_link_filter_function($post_link, $id = 0, $leavename = FALSE) {

    global $category_url_link;

    if (strpos('%auction_cat%', $post_link) === 'FALSE') {
        return $post_link;
    }
    $post = get_post($id);
    if (!is_object($post) || $post->post_type != 'auction') {

        if (AuctionTheme_using_permalinks())
            return str_replace("auction_cat", $category_url_link, $post_link);
        else
            return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, 'auction_cat');
    if (!$terms) {
        return str_replace('%auction_cat%', 'uncategorized', $post_link);
    }
    return str_replace('%auction_cat%', $terms[0]->slug, $post_link);
}

add_filter('term_link', 'AuctionTheme_post_tax_link_filter_function', 1, 3);
/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_post_tax_link_filter_function($post_link, $id = 0, $leavename = FALSE) {
    global $category_url_link;

    if (!AuctionTheme_using_permalinks())
        return $post_link;
    return str_replace("auction_cat", $category_url_link, $post_link);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_avatar($uid, $w = 25, $h = 25) {
    $av = get_user_meta($uid, 'avatar', true);
    if (empty($av))
        return get_bloginfo('template_url') . "/images/noav.jpg";
    else
        return AuctionTheme_generate_thumb($av, $w, $h);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_slider_post() {
    do_action('auctionTheme_slider_post');
}

add_filter('auctionTheme_slider_post', 'auctionTheme_slider_post_function');

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_slider_post_function() {
    ?>

    <div class="slider-post">
        <a href="<?php the_permalink(); ?>"><img width="150" height="110" class="image_class3" 
                                                 src="<?php echo AuctionTheme_get_first_post_image(get_the_ID(), 150, 110); ?>" alt="<?php the_title(); ?>" /></a>
        <br/>

        <p><span class="slider-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php
    the_title();
    ?></a></span><br/>
    <?php echo get_the_term_list(get_the_ID(), 'auction_location', '', ', ', ''); ?><br/>
    <?php echo auctionTheme_get_show_price(auctionTheme_get_current_price(get_the_ID())); ?><br/><br/>


            <a href="<?php the_permalink(); ?>" class="buttonlight"><?php _e('Buy Now', 'AuctionTheme'); ?></a>

        </p>

    </div>

    <?php
}

add_filter('auctionTheme_get_post', 'auctionTheme_get_post_function', 0, 1);

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_post($arr = '') {
    do_action('auctionTheme_get_post', $arr);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_post_function($arr = '') {

    if ($arr[0] == "winner")
        $pay_this_me = 1;
    if ($arr[0] == "unpaid")
        $unpaid = 1;

    $paid = get_post_meta(get_the_ID(), 'paid', true);

    $ending = get_post_meta(get_the_ID(), 'ending', true);
    $sec = $ending - current_time('timestamp', 0);
    $location = get_post_meta(get_the_ID(), 'Location', true);
    $closed = get_post_meta(get_the_ID(), 'closed', true);
    $post = get_post(get_the_ID());
    $only_buy_now = get_post_meta(get_the_ID(), 'only_buy_now', true);
    $buy_now = get_post_meta(get_the_ID(), 'buy_now', true);
    $featured = get_post_meta(get_the_ID(), 'featured', true);
    //$current_bid 		= get_post_meta(get_the_ID(), 'current_bid', true);

    $post = get_post(get_the_ID());

    global $current_user;
    get_currentuserinfo();
    $uid = $current_user->ID;

    $pid = get_the_ID();
    ?>                   

    <div class="datcuren">

        <p class="expiration_auction_p">
    <?php echo ($closed == "1" ? __('Closed', 'AuctionTheme') : ($ending - current_time('timestamp', 0))); ?>
        </p>

    <!--<p><?php echo ($closed == "1" ? __('Closed', 'AuctionTheme') : AuctionTheme_prepare_seconds_to_words($ending - current_time('timestamp', 0))); ?></p> -->
    </div> 
    <p class="author"><?php echo __("Auhor:", 'AuctionTheme'); ?> 
        <a href="<?php echo AuctionTheme_get_user_profile_link($author->ID); ?>"><?php the_author(); ?></a>
        <br/>


    <?php
    if ($post->post_status == "draft" && $paid == 1) {

        echo '<br/><span class="awaiting_moderation">' . __('Awaiting admin moderation', 'AuctionTheme') . '</span>';
    }
    ?>

    </p>


    <div class="post" id="post-ID-<?php the_ID(); ?>">


    <?php if ($featured == "1"): ?>
            <div class="featured-two"></div>
    <?php endif; ?>



        <div class="padd10">

            <div class="image_holder">
                <a href="<?php the_permalink(); ?>"><img width="200" height="200" class="image_class" 
                                                         alt="<?php the_title(); ?>" src="<?php echo AuctionTheme_get_first_post_image(get_the_ID(), 75, 65); ?>" /></a>



            </div>
            <div class="imgg">
    <?php
    $arr = AuctionTheme_get_post_images(get_the_ID());

    if ($arr) {


        echo '<ul class="imgh">';
        foreach ($arr as $image) {
            echo '<li><a href="' . AuctionTheme_generate_thumb($image, -1, 600) . '" rel="image_gal2"><img src="' . AuctionTheme_generate_thumb($image, 100, 80) . '" 
					class="img_class" /></a></li>';
        }
        echo '</ul>';
    } else {
        echo __('No images.', 'AuctionTheme');
    }
    ?>
            </div>


            <div  class="title_holder" > 
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php
    the_title();
    ?></a></h2>


    <?php
    if (!AuctionTheme_is_different_home_layout()) {

        $author = get_userdata($post->post_author);
        ?>     



                    <!-- ############### -->    
                    <div>


        <?php if ($pay_this_me == 1): ?>
                            <a href="<?php bloginfo('siteurl'); ?>/my-account/pay-for-auction/<?php echo get_the_ID(); ?>" 
                               class="post_bid_btn"><?php echo __("Pay This", "AuctionTheme"); ?></a>
        <?php endif; ?>

        <?php if (!AuctionTheme_is_different_home_layout()) { ?>  

            <?php if ($pay_this_me != 1): ?>
                                <a href="<?php the_permalink(); ?>" class="post_bid_btn"><?php echo __("Read More", "AuctionTheme"); ?></a>
            <?php endif; ?>

            <?php if ($paid != 1 and ($post->post_author == $uid)): ?>
                                <a href="<?php echo AuctionTheme_post_new_with_pid_stuff_thg(get_the_ID(), 3); ?>" class="post_bid_btn"><?php echo __("Publish", "AuctionTheme"); ?></a>
            <?php endif; ?>




            <?php if ($post->post_author == $uid) { ?>
                                <a href="<?php bloginfo('siteurl') ?>/?a_action=edit_auction&pid=<?php the_ID(); ?>" class="post_bid_btn"><?php echo __("Edit Auction", "AuctionTheme"); ?></a>
            <?php } ?>

                            <?php if ($post->post_author == $uid) { //$closed == 1)  ?> 

                                <?php if ($closed == "1") { //$closed == 1)  ?>
                                    <a href="<?php bloginfo('siteurl') ?>/?a_action=relist_auction&pid=<?php the_ID(); ?>" class="post_bid_btn"><?php echo __("Repost Auction", "AuctionTheme"); ?></a>

                                <?php
                                } /* } else { */

                                if (auctionTheme_has_1_bid($pid) == false):
                                    ?>

                                    <a href="<?php bloginfo('siteurl') ?>/?a_action=delete_auction&pid=<?php the_ID(); ?>" class="post_bid_btn_err"><?php echo __("Delete", "AuctionTheme"); ?></a>

                <?php endif;
            } ?>

        <?php } ?>



                    </div>
                    <!-- ############### -->

                </div> 

    <?php } ?>

            <div class="details_holder"> 


                <ul class="auction-details1">
                    <li>
                        <p><?php echo auctionTheme_get_show_price(auctionTheme_get_current_price(get_the_ID())); ?>
    <?php if ($only_buy_now == '1') : ?>

                                [<?php _e("BuyNow", 'AuctionTheme'); ?>]                                
    <?php endif; ?> 
                        </p>/
                    </li>

    <?php if ($only_buy_now != '1') : ?>



        <?php if (!empty($buy_now)): ?>

                            <li>
                                <h3><?php echo __("Buy Now", 'AuctionTheme'); ?>:</h3>
                                <p><?php echo auctionTheme_get_show_price($buy_now); ?></p>/
                            </li>

        <?php endif; ?>


                        <li>

                            <p><?php echo auctionTheme_number_of_bid(get_the_ID()); ?></p>/ 
                        </li> 

    <?php endif; ?>    

                    <li>
    <?php echo get_the_term_list(get_the_ID(), 'auction_cat', '', ', ', ''); ?>

                    </li>   											

                </ul>


            </div>   

        </div>
           
    </div>
    <div class="ft">
        <table width="350">
        <tr>
            <td width="60"><a href="#">Timeline</a></td>
            <td><a href="#">Serch</a></td>
            <td><a href="#">Dislay</a></td>
            <td><a href="#">Nottificate</a></td>
            <td><a href="<?php echo AuctionTheme_my_account_link(); ?>">Mypage</a></td>
        </tr>
    </table>
    </div>
    

        <?php
    }

    /*     * ***************************************************************************
     *
     * 	Function - AuctionTheme -
     *
     * *************************************************************************** */

    function AuctionTheme_get_item_primary_cat($pid) {
        $auction_cat = wp_get_object_terms($pid, 'auction_cat');
        if (is_array($auction_cat)) {
            return $auction_cat[0]->term_id;
        }

        return 0;
    }

    /*     * ***************************************************************************
     *
     * 	Function - AuctionTheme -
     *
     * *************************************************************************** */
    add_filter('auctionTheme_get_post_not_shipped', 'auctionTheme_get_post_not_shipped_function');

    function auctionTheme_get_post_not_shipped() {
        do_action('auctionTheme_get_post_not_shipped');
    }

    /*     * ***************************************************************************
     *
     * 	Function - AuctionTheme -
     *
     * *************************************************************************** */

    function auctionTheme_get_post_not_shipped_function() {


        $ending = get_post_meta(get_the_ID(), 'ending', true);
        $sec = $ending - current_time('timestamp', 0);
        $location = get_post_meta(get_the_ID(), 'Location', true);
        $closed = get_post_meta(get_the_ID(), 'closed', true);
        $only_buy_now = get_post_meta(get_the_ID(), 'only_buy_now', true);
        $buy_now = get_post_meta(get_the_ID(), 'buy_now', true);
        $featured = get_post_meta(get_the_ID(), 'featured', true);

        global $current_user, $post;
        get_currentuserinfo();
        $uid = $current_user->ID;

        $pid = get_the_ID();


        $bid = auctionTheme_get_winner_bid($post->bid_id);
        $winner = get_userdata($bid->uid);
        ?>
    <div class="post" id="post-ID-<?php the_ID(); ?>">


                        <?php if ($featured == "1"): ?>
            <div class="featured-two"></div>
                        <?php endif; ?>



        <div class="padd10">
            <div class="image_holder">
                <a href="<?php the_permalink(); ?>"><img width="75" height="65" class="image_class" 
                                                         alt="<?php the_title(); ?>" src="<?php echo AuctionTheme_get_first_post_image(get_the_ID(), 75, 65); ?>" /></a>

                <div class="watch-list">
    <?php
    if (AuctionTheme_check_if_pid_is_in_watchlist(get_the_ID(), $uid) == true):
        ?>

                        <a class="rem-to-watchlist" rel="<?php the_ID(); ?>"  href="#"><?php _e('- watchlist', 'AuctionTheme'); ?></a>

                    <?php else: ?>

                        <a class="add-to-watchlist" rel="<?php the_ID(); ?>" href="#"><?php _e('+ watchlist', 'AuctionTheme'); ?></a>
                       <?php endif; ?>   


                </div>   
            </div>
            <div  class="title_holder" > 
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                    <?php the_title(); ?></a></h2>


                <p class="mypostedon"><?php echo __("Posted on", 'AuctionTheme'); ?> <?php the_time('F jS, Y'); ?>  by 
                    <a href="<?php echo AuctionTheme_get_user_profile_link($author->ID); ?>"><?php the_author(); ?></a>
                    <br/>
    <?php _e("Posted in", "AuctionTheme"); ?> <?php echo get_the_term_list(get_the_ID(), 'auction_cat', '', ', ', ''); ?> </p>

                <div>


                    <p class="shipping_info">
                    <?php
                    $shp = get_user_meta($winner->ID, 'shipping_info', true);
                    printf(__('Buyer Shipping Info: %s', 'AuctionTheme'), $shp);
                    ?>
                    </p>    



                    <a href="<?php the_permalink(); ?>" class="post_bid_btn"><?php echo __("Read More", "AuctionTheme"); ?></a>

                    <a href="<?php bloginfo('siteurl'); ?>/?a_action=mark_shipped&bid_id=<?php echo $post->bid_id; ?>" class="post_bid_btn"><?php echo __("Mark as Shipped", "AuctionTheme"); ?></a>







                </div> </div>


            <div class="details_holder"> 


                <ul class="auction-details1">

                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/price.png" alt="price" width="15" height="15" /> 
                        <h3><?php echo __("Price", 'AuctionTheme'); ?>:</h3>
                        <p><?php echo auctionTheme_get_show_price($bid->bid); ?></p>
                    </li>

                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/posted.png" alt="price" width="15" height="15" /> 
                        <h3><?php echo __("Winner", 'AuctionTheme'); ?>:</h3>
                        <p><?php echo "<a href='" . AuctionTheme_get_user_profile_link($bid->uid) . "'>" . $winner->user_login . "</a>"; ?></p>
                    </li>	

                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/cal.png" alt="price" width="15" height="15" /> 
                        <h3><?php echo __("Aquired On", 'AuctionTheme'); ?>:</h3>
                        <p><?php echo date_i18n('d-M-Y H:i:s', $bid->date_choosen); ?></p>
                    </li>	

                </ul>


            </div>   

        </div> </div>
    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_post_outstanding_payment($bid_id) {

    if ($arr[0] == "winner")
        $pay_this_me = 1;
    if ($arr[0] == "unpaid")
        $unpaid = 1;

    $ending = get_post_meta(get_the_ID(), 'ending', true);
    $sec = $ending - current_time('timestamp', 0);
    $location = get_post_meta(get_the_ID(), 'Location', true);
    $closed = get_post_meta(get_the_ID(), 'closed', true);
    $post = get_post(get_the_ID());
    $only_buy_now = get_post_meta(get_the_ID(), 'only_buy_now', true);
    $buy_now = get_post_meta(get_the_ID(), 'buy_now', true);
    $featured = get_post_meta(get_the_ID(), 'featured', true);



    global $current_user;
    get_currentuserinfo();
    $uid = $current_user->ID;

    $pid = get_the_ID();

    global $wpdb;

    $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
    $r = $wpdb->get_results($s);
    $bid = $r[0];
    ?>
    <div class="post" id="post-ID-<?php the_ID(); ?>">


    <?php if ($featured == "1"): ?>
            <div class="featured-two"></div>
    <?php endif; ?>



        <div class="padd10">
            <div class="image_holder">
                <a href="<?php the_permalink(); ?>"><img width="75" height="65" class="image_class" 
                                                         alt="<?php the_title(); ?>" src="<?php echo AuctionTheme_get_first_post_image(get_the_ID(), 75, 65); ?>" /></a>

                <div class="watch-list">
    <?php
    if (AuctionTheme_check_if_pid_is_in_watchlist(get_the_ID(), $uid)):
        ?>

                        <a class="rem-to-watchlist" rel="<?php the_ID(); ?>"  href="#"><?php _e('- watchlist', 'AuctionTheme'); ?></a>

    <?php else: ?>

                        <a class="add-to-watchlist" rel="<?php the_ID(); ?>" href="#"><?php _e('+ watchlist', 'AuctionTheme'); ?></a>
    <?php endif; ?>   


                </div>   
            </div>
            <div  class="title_holder" > 
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>


                <p class="mypostedon"><?php echo __("Posted on", 'AuctionTheme'); ?> <?php the_time('F jS, Y'); ?>. <?php
    printf(__('Seller: %s', 'AuctionTheme'), '<a href="' . auctionTheme_get_user_profile_link($post->post_author) . '">' . get_the_author() . '</a>'
    );
    ?> 

                    <br/>
    <?php _e("Posted in", "AuctionTheme"); ?> <?php echo get_the_term_list(get_the_ID(), 'auction_cat', '', ', ', ''); ?> </p>

                <!-- ############### -->    
                <div>

                    <a href="<?php echo auctionTheme_pay_for_item_link($bid_id); ?>" 
                       class="post_bid_btn"><?php echo __("Pay This", "AuctionTheme"); ?></a>


                </div>
                <!-- ############### -->

            </div> 

            <div class="details_holder"> 




                <ul class="auction-details1">
                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/price.png" alt="price" width="15" height="15" /> 
                        <h3><?php echo __("Price", 'AuctionTheme'); ?>:</h3>
                        <p><?php echo auctionTheme_get_show_price($bid->bid); ?>           </p>
                    </li>


                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/cal.png" width="15" alt="posted" height="15" /> 
                        <h3><?php echo __("Posted on", 'AuctionTheme'); ?>:</h3>
                        <p><?php the_time("j F Y g:i A"); ?></p>
                    </li>


                    <li>
                        <img src="<?php echo get_bloginfo('template_url'); ?>/images/cal.png" width="15" alt="posted" height="15" /> 
                        <h3><?php echo __("Purchased", 'AuctionTheme'); ?>:</h3>
                        <p><?php echo date_i18n("j F Y g:i A", $bid->date_choosen); ?></p>
                    </li>


                </ul>


            </div>   

        </div></div>
    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_first_post_image($pid, $w = 100, $h = 100) {

    //---------------------
    // build the exclude list
    $exclude = array();

    $args = array(
        'order' => 'ASC',
        'post_type' => 'attachment',
        'post_parent' => get_the_ID(),
        'meta_key' => 'another_reserved1',
        'meta_value' => '1',
        'numberposts' => -1,
        'post_status' => null,
    );
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $url = $attachment->ID;
            array_push($exclude, $url);
        }
    }

    //-----------------

    $args = array(
        'order' => 'ASC',
        'orderby' => 'post_date',
        'post_type' => 'attachment',
        'post_parent' => $pid,
        'exclude' => $exclude,
        'post_mime_type' => 'image',
        'post_status' => null,
        'numberposts' => 1,
    );
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $url = wp_get_attachment_url($attachment->ID);
            return AuctionTheme_generate_thumb($url, $w, $h);
        }
    } else {
        return get_bloginfo('template_url') . '/images/nopic.png';
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_custom_fields_html() {
    global $post, $wpdb;
    $pid = $post->ID;
    ?>
    <table width="100%">
        <input type="hidden" value="1" name="fromadmin" />
    <?php
    $cat = wp_get_object_terms($pid, 'auction_cat');
    $catidarr = array();

    foreach ($cat as $catids) {
        $catidarr[] = $catids->term_id;
    }

    $arr = get_auction_category_fields($catidarr, $pid);

    for ($i = 0; $i < count($arr); $i++) {

        echo '<tr>';
        echo '<td>' . $arr[$i]['field_name'] . $arr[$i]['id'] . ':</td>';
        echo '<td>' . $arr[$i]['value'];
        do_action('AuctionTheme_step3_after_custom_field_' . $arr[$i]['id'] . '_field');
        echo '</td>';
        echo '</tr>';
    }
    ?> 


    </table>
    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_theme_auction_bids() {
    global $post;
    $pid = $post->ID;


    $closed = get_post_meta($pid, 'closed', true);
    $post = get_post($pid);
    global $wpdb;


    $bids = "select * from " . $wpdb->prefix . "auction_bids where pid='$pid' order by id DESC";
    $res = $wpdb->get_results($bids);



    if (count($res) > 0) {



        echo '<table width="100%">';
        echo '<thead><tr>';
        echo '<th>' . __('Username', 'AuctionTheme') . '</th>';
        echo '<th>' . __('Bid Amount', 'AuctionTheme') . '</th>';
        echo '<th>' . __('Date Made', 'AuctionTheme') . '</th>';

        echo '<th>' . __('Winner', 'AuctionTheme') . '</th>';
        echo '<th>' . __('Options', 'AuctionTheme') . '</th>';

        echo '</tr></thead><tbody>';


        //-------------

        foreach ($res as $row) {


            $user = get_userdata($row->uid);
            echo '<tr>';
            echo '<th>' . $user->user_login . '</th>';
            echo '<th>' . auctionTheme_get_show_price($row->bid) . '</th>';
            echo '<th>' . date("d-M-Y H:i:s", $row->date_made) . '</th>';


            if ($row->winner == 1)
                echo '<th>Yes</th>'; else
                echo '<th>&nbsp;</th>';

            echo '<th><a href="">Delete</a></th>';

            echo '</tr>';
        }

        echo '</tbody></table>';
    }
    else
        _e("No bids placed yet.");
    ?>	

    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_theme_auction_dts() {
    global $post;
    $pid = $post->ID;
    $price = get_post_meta($pid, "price", true);
    $location = get_post_meta($pid, "Location", true);
    $f = get_post_meta($pid, "featured", true);
    $t = get_post_meta($pid, "closed", true);

    $reverse = get_post_meta($pid, "reverse", true);
    ?>

    <ul id="post-new4"> 
        <input name="fromadmin" type="hidden" value="1" />

        <li>
            <h2><?php echo __('Address', 'AuctionTheme'); ?>:</h2>
            <p><input type="text" size="15" name="Location" class="do_input" 
                      value="<?php echo get_post_meta($pid, 'Location', true); ?>" /></p>
        </li>

    <?php if ($reverse == "yes" || $reverse == "1") : ?>

            <li>
                <h2><?php echo __('Price', 'AuctionTheme'); ?>:</h2>
                <p><input type="text" size="10" name="price" class="do_input" 
                          value="<?php echo get_post_meta($pid, 'price', true); ?>" /> <?php echo auctionTheme_currency(); ?></p>
            </li>

    <?php else: ?>

            <li>
                <h2><?php echo __('Start Price', 'AuctionTheme'); ?>:</h2>
                <p><input type="text" size="10" name="start_price" class="do_input" 
                          value="<?php echo get_post_meta($pid, 'start_price', true); ?>" /> 
        <?php echo auctionTheme_currency(); ?></p>
            </li>


            <li>
                <h2><?php echo __('Reserve Price', 'AuctionTheme'); ?>:</h2>
                <p><input type="text" size="10" name="reserve" class="do_input" 
                          value="<?php echo get_post_meta($pid, 'reserve', true); ?>" /> 
        <?php echo auctionTheme_currency(); ?></p>
            </li>


            <li>
                <h2><?php echo __('Buy Now Price', 'AuctionTheme'); ?>:</h2>
                <p><input type="text" size="10" name="buy_now" class="do_input" 
                          value="<?php echo get_post_meta($pid, 'buy_now', true); ?>" /> <?php echo auctionTheme_currency(); ?>

                    <input name="only_buy_now" value="1" type="checkbox" <?php if (get_post_meta($pid, 'only_buy_now', true) == "1")
            echo 'checked="checked"'; ?> /> 
        <?php _e('Only buy now auction', 'AuctionTheme'); ?>

                </p>
            </li>

            <li>
                <h2><?php echo __('Quantity', 'AuctionTheme'); ?>:</h2>
                <p><input type="text" size="10" name="quant" class="do_input" 
                          value="<?php echo get_post_meta($pid, 'quant', true); ?>" /></p>
            </li>



    <?php endif; ?>

        <li>
            <h2><?php echo __('Shipping Cost', 'AuctionTheme'); ?>:</h2>
            <p><input type="text" size="10" name="shipping" class="do_input" 
                      value="<?php echo get_post_meta($pid, 'shipping', true); ?>" /> <?php echo auctionTheme_currency(); ?></p>
        </li>


        <li>
            <h2><?php echo __('Private Bids', 'AuctionTheme'); ?>:</h2>
            <p><select name="private_bids">
                    <option value="no" <?php if (get_post_meta($pid, 'private_bids', true) == "no")
        echo 'selected="selected"'; ?>><?php _e("No", 'AuctionTheme'); ?></option>
                    <option value="yes" <?php if (get_post_meta($pid, 'private_bids', true) == "yes")
        echo 'selected="selected"'; ?>><?php _e("Yes", 'AuctionTheme'); ?></option>

                </select>
            </p>
        </li>

        <li>
            <h2><?php _e("Feature this auction", 'AuctionTheme'); ?>:</h2>
            <p><input type="checkbox" value="1" name="featureds" <?php if ($f == '1')
        echo ' checked="checked" '; ?> /></p>
        </li>


        <li>
            <h2><?php _e("Closed", 'AuctionTheme'); ?>:</h2>
            <p><input type="checkbox" value="1" name="closed" <?php if ($t == '1')
        echo ' checked="checked" '; ?> /></p>
        </li>




        <li>
            <h2>


                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>


                <link rel="stylesheet" media="all" type="text/css" href="<?php echo get_bloginfo('template_url'); ?>/css/ui-thing.css" />
                <script type="text/javascript" language="javascript" src="<?php echo get_bloginfo('template_url'); ?>/js/jquery-ui-timepicker-addon.js"></script>




        <?php _e("Auction Ending On", 'AuctionTheme'); ?>:</h2>
            <p><input type="text" name="ending" id="ending" value="<?php
    $d = get_post_meta($pid, 'ending', true);

    if (!empty($d)) {
        $r = date('m/d/Y H:i:s', $d);
        echo $r;
    }
        ?>" class="do_input"  /></p>
        </li>

        <script>

            $(document).ready(function() {
                $('#ending').datetimepicker({
                    showSecond: true,
                    timeFormat: 'hh:mm:ss'
                });});
     
        </script>


    </ul>    


    <?php
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_generate_thumb($img_url, $width, $height, $cut = true) {


    require_once(ABSPATH . '/wp-admin/includes/image.php');
    $uploads = wp_upload_dir();
    $basedir = $uploads['basedir'] . '/';
    $exp = explode('/', $img_url);

    $nr = count($exp);
    $pic = $exp[$nr - 1];
    $year = $exp[$nr - 3];
    $month = $exp[$nr - 2];

    if ($uploads['basedir'] == $uploads['path']) {
        $img_url = $basedir . '/' . $pic;
        $ba = $basedir . '/';
        $iii = $uploads['url'];
    } else {
        $img_url = $basedir . $year . '/' . $month . '/' . $pic;
        $ba = $basedir . $year . '/' . $month . '/';
        $iii = $uploads['baseurl'] . "/" . $year . "/" . $month;
    }
    list($width1, $height1, $type1, $attr1) = getimagesize($img_url);

    //return $height;
    $a = false;
    if ($width == -1) {
        $a = true;
    }


    if ($width > $width1)
        $width = $width1 - 1;
    if ($height > $height1)
        $height = $height1 - 1;

    if ($a == true) {
        $prop = $width1 / $height1;
        $width = round($prop * $height);
    }

    $width = $width - 1;
    $height = $height - 1;


    $xxo = "-" . $width . "x" . $height;
    $exp = explode(".", $pic);
    $new_name = $exp[0] . $xxo . "." . $exp[1];

    $tgh = str_replace("//", "/", $ba . $new_name);

    if (file_exists($tgh))
        return $iii . "/" . $new_name;



    $thumb = image_resize($img_url, $width, $height, $cut);

    if (is_wp_error($thumb))
        return "is-wp-error";

    $exp = explode($basedir, $thumb);
    return $uploads['baseurl'] . "/" . $exp[1];
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_is_different_home_layout() {
    //$AuctionTheme_home_page_layout = get_option('AuctionTheme_home_page_layout');
    return false;
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_currency() {
    $c = trim(get_option('AuctionTheme_currency_symbol'));
    if (empty($c))
        return get_option('AuctionTheme_currency');
    return $c;
}

function auctionTheme_currency() {
    return AuctionTheme_get_currency();
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_get_show_price($price, $cents = 2) {
    $AuctionTheme_currency_position = get_option('AuctionTheme_currency_position');
    if ($AuctionTheme_currency_position == "front")
        return AuctionTheme_get_currency() . "" . AuctionTheme_formats($price, $cents);
    return AuctionTheme_formats($price, $cents) . "" . AuctionTheme_get_currency();
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_formats($number, $cents = 1) { // cents: 0=never, 1=if needed, 2=always
    $dec_sep = get_option('AuctionTheme_decimal_sum_separator');
    if (empty($dec_sep))
        $dec_sep = '.';

    $tho_sep = get_option('AuctionTheme_thousands_sum_separator');
    if (empty($tho_sep))
        $tho_sep = ',';

    //dec,thou

    if (is_numeric($number)) { // a number
        if (!$number) { // zero
            $money = ($cents == 2 ? '0' . $dec_sep . '00' : '0'); // output zero
        } else { // value
            if (floor($number) == $number) { // whole number
                $money = number_format($number, ($cents == 2 ? 2 : 0), $dec_sep, $tho_sep); // format
            } else { // cents
                $money = number_format(round($number, 2), ($cents == 0 ? 0 : 2), $dec_sep, $tho_sep); // format
            } // integer or decimal
        } // value
        return $money;
    } // numeric
}

// formatMoney

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_prepare_seconds_to_words($seconds) {
    $res = AuctionTheme_seconds_to_words_new($seconds);
    if ($res == "Expired")
        return __('Expired', 'AuctionTheme');

    if ($res[0] == 0)
        return sprintf(__("%s hours, %s min, %s sec", 'AuctionTheme'), $res[1], $res[2], $res[3]);
    if ($res[0] == 1) {

        $plural = $res[1] > 1 ? __('days', 'AuctionTheme') : __('day', 'AuctionTheme');
        return sprintf(__("%s %s, %s hours, %s min", 'AuctionTheme'), $res[1], $plural, $res[2], $res[3]);
    }
}

/* * ***********************************************************
 *
 * 	AuctionTheme (c) sitemile.com - function
 *
 * ************************************************************ */

function AuctionTheme_seconds_to_words_new($seconds) {
    if ($seconds < 0)
        return 'Expired';

    /*     * * number of days ** */
    $days = (int) ($seconds / 86400);
    /*     * * if more than one day ** */
    $plural = $days > 1 ? 'days' : 'day';
    /*     * * number of hours ** */
    $hours = (int) (($seconds - ($days * 86400)) / 3600);
    /*     * * number of mins ** */
    $mins = (int) (($seconds - $days * 86400 - $hours * 3600) / 60);
    /*     * * number of seconds ** */
    $secs = (int) ($seconds - ($days * 86400) - ($hours * 3600) - ($mins * 60));
    /*     * * return the string ** */
    if ($days == 0 || $days < 0) {
        $arr[0] = 0;
        $arr[1] = $hours;
        $arr[2] = $mins;
        $arr[3] = $secs;
        return $arr; //sprintf("%d hours, %d min, %d sec", $hours, $mins, $secs);
    } else {
        $arr[0] = 1;
        $arr[1] = $days;
        $arr[2] = $hours;
        $arr[3] = $mins;

        return $arr; //sprintf("%d $plural, %d hours, %d min", $days, $hours, $mins);
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_number_of_bid($pid) {
    global $wpdb;
    $s = "select bid from " . $wpdb->prefix . "auction_bids where pid='$pid'";
    $r = $wpdb->get_results($s);

    return count($r);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_bid_values($pid) {
    global $wpdb;
    $s = "select bid from " . $wpdb->prefix . "auction_bids where pid='$pid' order by bid desc";
    $r = $wpdb->get_results($s);

    return $r;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_current_price($pid = '') {
    if (empty($pid))
        $pid = get_the_ID();
    $only_buy_now = get_post_meta($pid, 'only_buy_now', true);

    if ($only_buy_now == '1')
        return get_post_meta($pid, 'buy_now', true);

    $reverse = get_post_meta($pid, "reverse", true);
    if ($reverse == "yes") {
        $bids = auctionTheme_get_bid_values($pid);

        if (count($bids) == 0) {
            $start = auctionTheme_get_start_price($pid);
            return ($start == false ? 0 : $start );
        } else {
            return auctionTheme_get_lowest_bid($pid);
        }
    } //return get_post_meta($pid, 'price', true);
    else {
        $bids = auctionTheme_get_bid_values($pid);

        if (count($bids) == 0) {
            $start = auctionTheme_get_start_price($pid);
            return ($start == false ? 0 : $start );
        } else {
            return auctionTheme_get_highest_bid($pid);
        }
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_start_price($pid = '') {
    if (empty($pid))
        $pid = get_the_ID();
    $price = get_post_meta($pid, 'start_price', true);

    if (empty($price))
        $price = false;
    return $price;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_auction_category_fields_without_vals($catid, $clas_op = '') {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_custom_fields order by ordr asc";
    $r = $wpdb->get_results($s);

    $arr1 = array();
    $i = 0;

    if ($clas_op != "no")
        $clas_op = 'do_input';

    foreach ($r as $row) {
        $ims = $row->id;
        $name = $row->name;
        $tp = $row->tp;

        if ($row->cate == 'all') {
            $arr1[$i]['id'] = $ims;
            $arr1[$i]['name'] = $name;
            $arr1[$i]['tp'] = $tp;
            $i++;
        } else {
            $se = "select * from " . $wpdb->prefix . "auction_custom_relations where custid='$ims'";
            $re = $wpdb->get_results($se);

            if (count($re) > 0)
                foreach ($re as $rowe) { // = mysql_fetch_object($re))
                    if (count($catid) > 0)
                        foreach ($catid as $id_of_cat) {

                            if ($rowe->catid == $id_of_cat) {
                                $flag_me = 1;
                                for ($k = 0; $k < count($arr1); $k++) {
                                    if ($arr1[$k]['id'] == $ims) {
                                        $flag_me = 0;
                                        break;
                                    }
                                }

                                if ($flag_me == 1) {
                                    $arr1[$i]['id'] = $ims;
                                    $arr1[$i]['name'] = $name;
                                    $arr1[$i]['tp'] = $tp;
                                    $i++;
                                }
                            }
                        }
                }
        }
    }

    $arr = array();
    $i = 0;

    for ($j = 0; $j < count($arr1); $j++) {
        $ids = $arr1[$j]['id'];
        $tp = $arr1[$j]['tp'];

        $arr[$i]['field_name'] = $arr1[$j]['name'];
        $arr[$i]['id'] = '<input type="hidden" value="' . $ids . '" name="custom_field_id[]" />';

        if ($tp == 1) {

            $teka = ''; //!empty($pid) ? get_post_meta($pid, 'custom_field_ID_'.$ids, true) : "" ;

            $arr[$i]['value'] = '<input class="' . $clas_op . '" type="text" size="30" name="custom_field_value_' . $ids . '" 
		value="' . (isset($_GET['custom_field_value_' . $ids]) ? $_GET['custom_field_value_' . $ids] : $teka ) . '" />';
        }

        if ($tp == 5) {

            $teka = ''; //!empty($pid) ? get_post_meta($pid, 'custom_field_ID_'.$ids, true) : "" ;
            $value = isset($_GET['custom_field_value_' . $ids]) ? $_GET['custom_field_value_' . $ids] : $teka;

            $arr[$i]['value'] = '<textarea rows="5" cols="40" class="' . $clas_op . '" name="custom_field_value_' . $ids . '">' . $value . '</textarea>';
        }

        if ($tp == 3) { //radio
            $arr[$i]['value'] = '';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = ''; //!empty($pid) ? get_post_meta($pid, 'custom_field_ID_'.$ids, true) : "" ;
                    if (isset($_GET['custom_field_value_' . $ids])) {
                        if ($_GET['custom_field_value_' . $ids] == $row2->valval)
                            $value = 'checked="checked"';
                        else
                            $value = '';
                    }
                    elseif (!empty($pid)) {
                        $v = ''; //get_post_meta($pid, 'custom_field_ID_'.$ids, true);
                        if ($v == $row2->valval)
                            $value = 'checked="checked"';
                        else
                            $value = '';
                    }
                    else
                        $value = '';

                    $arr[$i]['value'] .= '<input type="radio" ' . $value . ' value="' . $row2->valval . '" name="custom_field_value_' . $ids . '"> ' . $row2->valval . '<br/>';
                }
        }


        if ($tp == 4) { //checkbox
            $arr[$i]['value'] = '';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = ''; //!empty($pid) ? get_post_meta($pid, 'custom_field_ID_'.$ids) : "" ;

                    if (!empty($teka)) {
                        foreach ($teka as $te) {
                            if ($te == $row2->valval) {
                                $teka = "checked='checked'";
                                break;
                            }
                        }

                        $teka = '';
                    }
                    else
                        $teka = '';


                    $value = isset($_GET['custom_field_value_' . $ids]) ? "checked='checked'" : $teka;

                    $arr[$i]['value'] .= '<input ' . $value . ' type="checkbox" value="' . $row2->valval . '" name="custom_field_value_' . $ids . '[]"> ' . $row2->valval . '<br/>';
                }
        }

        if ($tp == 2) { //select
            $arr[$i]['value'] = '<select class="' . $clas_op . '" name="custom_field_value_' . $ids . '" /><option value="">' . __('Select', 'AuctionTheme') . '</option>';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = ''; //!empty($pid) ? get_post_meta($pid, 'custom_field_ID_'.$ids) : "" ;

                    if (!empty($teka)) {
                        foreach ($teka as $te) {
                            if ($te == $row2->valval) {
                                $teka = "selected='selected'";
                                break;
                            }
                        }

                        $teka = '';
                    }
                    else
                        $teka = '';

                    if (isset($_GET['custom_field_value_' . $ids]) && $_GET['custom_field_value_' . $ids] == $row2->valval)
                        $value = "selected='selected'";
                    else
                        $value = $teka;


                    $arr[$i]['value'] .= '<option ' . $value . ' value="' . $row2->valval . '">' . $row2->valval . '</option>';
                }
            $arr[$i]['value'] .= '</select>';
        }

        $i++;
    }

    return $arr;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_highest_bid($pid) {
    global $wpdb;
    $s = "select bid from " . $wpdb->prefix . "auction_bids where pid='$pid' order by bid desc limit 1";
    $r = $wpdb->get_results($s);

    if (count($r) == 0) {
        $start_price = get_post_meta($pid, 'start_price', true);
        if (empty($start_price))
            return false;
        return $start_price;
    }


    $r = $r[0];
    return $r->bid;
}

function auctionTheme_has_1_bid($pid) {
    global $wpdb;
    $s = "select id from " . $wpdb->prefix . "auction_bids where pid='$pid' limit 1";
    $r = $wpdb->get_results($s);

    if (count($r) == 0) {
        return false;
    }

    return true;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_lowest_bid($pid) {
    global $wpdb;
    $s = "select bid from " . $wpdb->prefix . "auction_bids where pid='$pid' order by bid asc limit 1";
    $r = $wpdb->get_results($s);

    if (count($r) == 0) {
        $start_price = get_post_meta($pid, 'start_price', true);
        if (empty($start_price))
            return false;
        return $start_price;
    }


    $r = $r[0];
    return $r->bid;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_framework_init_widgets() {




    register_sidebar(array(
        'name' => __('Single Page Sidebar', 'AuctionTheme'),
        'id' => 'single-widget-area',
        'description' => __('The sidebar area of the single blog post', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));




    register_sidebar(array(
        'name' => __('AuctionTheme - Stretch Wide MainPage Sidebar', 'AuctionTheme'),
        'id' => 'main-stretch-area',
        'description' => __('This sidebar is site wide stretched in home page, just under the slider/menu.', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));



    register_sidebar(array(
        'name' => __('Other Page Sidebar', 'AuctionTheme'),
        'id' => 'other-page-area',
        'description' => __('The sidebar area of any other page than the defined ones', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));




    register_sidebar(array(
        'name' => __('Home Page Sidebar - Right', 'AuctionTheme'),
        'id' => 'home-right-widget-area',
        'description' => __('The right sidebar area of the homepage', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));




    register_sidebar(array(
        'name' => __('Home Page Sidebar - Left', 'AuctionTheme'),
        'id' => 'home-left-widget-area',
        'description' => __('The left sidebar area of the homepage', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));



    register_sidebar(array(
        'name' => __('First Footer Widget Area', 'AuctionTheme'),
        'id' => 'first-footer-widget-area',
        'description' => __('The first footer widget area', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    // Area 4, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Second Footer Widget Area', 'AuctionTheme'),
        'id' => 'second-footer-widget-area',
        'description' => __('The second footer widget area', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    // Area 5, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Third Footer Widget Area', 'AuctionTheme'),
        'id' => 'third-footer-widget-area',
        'description' => __('The third footer widget area', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));

    // Area 6, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Fourth Footer Widget Area', 'AuctionTheme'),
        'id' => 'fourth-footer-widget-area',
        'description' => __('The fourth footer widget area', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));



    register_sidebar(array(
        'name' => __('AuctionTheme - Auction Single Sidebar', 'AuctionTheme'),
        'id' => 'auction-widget-area',
        'description' => __('The sidebar of the single auction page', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));


    register_sidebar(array(
        'name' => __('AuctionTheme - HomePage Area', 'AuctionTheme'),
        'id' => 'main-page-widget-area',
        'description' => __('The sidebar for the main page, just under the slider.', 'AuctionTheme'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_is_home() {
    global $current_user, $wp_query;
    $a_action = $wp_query->query_vars['a_action'];

    if (!empty($a_action))
        return false;
    if (is_home())
        return true;
    return false;
}

function auctionTheme_coin_slider_home() {
    if (0) { //is_home())
        ?>
        <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_bloginfo('template_url'); ?>/css/skins.css" />
        <script type="text/javascript" src="<?php echo get_bloginfo('template_url'); ?>/js/jcar.js"></script>

        <script type="text/javascript">
        			
            var $ = jQuery;
        			
            jQuery(document).ready(function() {
                jQuery('#mycarousel').jcarousel({
                    wrap: 'circular',
                    scroll: 1,
                    visible: 7
                });
            });
        	
        </script>

        <?php
    }
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_unread_number_messages($uid) {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_pm where user='$uid' and rd='0'";
    $r = $wpdb->get_results($s);
    return count($r);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_users_links() {
    global $current_user, $wpdb;
    get_currentuserinfo();
    //$rd = AuctionTheme_get_unread_number_messages($current_user->ID);
    //if($rd > 0) $ssk = "<span class='notif_a'>".$rd."</span>"; else $ssk = '';

    $uid = $current_user->ID;

    //------------------

    $query = "select id from " . $wpdb->prefix . "auction_ratings where fromuser='$uid' AND awarded='0'";
    $r = $wpdb->get_results($query);
    $ttl_fdbks = count($r);

    if ($ttl_fdbks > 0)
        $ttl_fdbks2 = "<span class='notif_a'>" . $ttl_fdbks . "</span>";




    $rd = auctionTheme_get_unread_number_messages($current_user->ID);
    if ($rd > 0)
        $ssk = "<span class='notif_a'>" . $rd . '</span>';
    ?>

    <div id="content">
        <ul class="xoxo">

            <li class="widget-container widget_text"><h3 class="widget-title"><?php _e("My Account Menu", 'AuctionTheme'); ?></h3>
                <p>

                <ul id="my-account-admin-menu">
                    <li><a href="<?php echo auctionTheme_my_account_link(); ?>"><?php _e("MyAccount Home", 'AuctionTheme'); ?></a></li>

    <?php
    if (AuctionTheme_is_able_to_post_auctions()):
        ?>
                        <li><a href="<?php echo auctionTheme_post_new_link(); ?>"><?php _e("Post New Auction", 'AuctionTheme'); ?></a></li>

    <?php endif; ?>

    <?php
    $AuctionTheme_enable_pay_credits = get_option('AuctionTheme_enable_pay_credits');
    if ($AuctionTheme_enable_pay_credits != 'no'):
        ?>

                        <li><a href="<?php echo auctionTheme_get_payments_link(); ?>"><?php _e("Payments", 'AuctionTheme'); ?></a></li>

    <?php endif; ?>

                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_priv_mess_page_id')); ?>"><?php echo sprintf(__("Private Messages %s", 'AuctionTheme'), $ssk); ?></a></li>

                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_personal_info_page_id')); ?>"><?php _e("Personal Info", 'AuctionTheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_reviews_page_id')); ?>"><?php printf(__("Reviews/Feedback %s", 'AuctionTheme'), $ttl_fdbks2); ?></a></li>
                </ul>

                </p>
            </li>

            <!-- ###### -->
    <?php
    if (AuctionTheme_is_able_to_post_auctions()):
        ?>
                <li class="widget-container widget_text"><h3 class="widget-title"><?php _e("Seller Menu", 'AuctionTheme'); ?></h3>
                    <p>

                    <ul id="my-account-admin-menu_seller">

        <?php
        $querystr = "SELECT distinct wposts.ID , bids.id bid_id
					FROM $wpdb->posts wposts, " . $wpdb->prefix . "auction_bids bids 
					WHERE wposts.ID=bids.pid AND
					wposts.post_author='$uid' AND 
					bids.winner='1' AND 
					bids.shipped='0' AND
					bids.paid='0'";

        $r = $wpdb->get_results($querystr);
        $rd = count($r);
        if ($rd > 0)
            $outs = "<span class='notif_a'>" . $rd . '</span>'; else
            $outs = '';

        //--------------------------------------------------------------------------------

        $querystr = "SELECT distinct wposts.ID, bids.id bid_id 
					FROM $wpdb->posts wposts, " . $wpdb->prefix . "auction_bids bids 
					WHERE wposts.ID=bids.pid AND
					wposts.post_author='$uid' AND 
					bids.winner='1' AND 
					bids.shipped='0' AND
					bids.paid='1'";

        $r = $wpdb->get_results($querystr);
        $rd1 = count($r);
        if ($rd1 > 0)
            $outs1 = "<span class='notif_a'>" . $rd1 . '</span>'; else
            $outs1 = '';
        ?>


                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_active_auctions_id')); ?>"><?php _e("Active Auctions", 'AuctionTheme'); ?></a></li>
                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_sold_auctions_id')); ?>"><?php _e("Sold Items", 'AuctionTheme'); ?></a></li>
                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_closed_auctions_id')); ?>"><?php _e("Closed Auctions", 'AuctionTheme'); ?></a></li>

                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_unpub_auctions_id')); ?>"><?php _e("Unpublished Auctions", 'AuctionTheme'); ?></a></li>                    
                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_aw_pay_auctions_id')); ?>"><?php printf(__("Awaiting Payment Items %s", 'AuctionTheme'), $outs); ?></a></li>
                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_not_shipped_auctions_id')); ?>"><?php printf(__("Not Shipped Items %s", 'AuctionTheme'), $outs1); ?></a></li>
                        <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_shipped_auctions_id')); ?>"><?php _e("Shipped Items", 'AuctionTheme'); ?></a></li>

                    </ul>

                    </p>
                </li>
    <?php endif; ?>
            <!-- ###### -->

            <li class="widget-container widget_text"><h3 class="widget-title"><?php _e("Buyer Menu", 'AuctionTheme'); ?></h3>
                <p>
    <?php
    global $wpdb;
    $querystr = "
					SELECT distinct wposts.ID , bids.id bid_id
					FROM $wpdb->posts wposts, " . $wpdb->prefix . "auction_bids bids 
					WHERE 					
					wposts.ID=bids.pid AND
					bids.uid='$uid' AND 
					bids.winner='1' AND 
					bids.shipped='0' AND
					bids.paid='0' ";

    $r = $wpdb->get_results($querystr);

    $rd = count($r);

    if ($rd > 0)
        $outs = "<span class='notif_a'>" . $rd . '</span>'; else
        $outs = '';
    ?>
                <ul id="my-account-admin-menu_buyer">

                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_won_items_id')); ?>"><?php _e("Won Items", 'AuctionTheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_not_won_items_id')); ?>"><?php _e("Did not Win Items", 'AuctionTheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_bid_items_id')); ?>"><?php _e("Items I bid", 'AuctionTheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_otstanding_pay_id')); ?>"><?php printf(__("Outstanding Payments %s", 'AuctionTheme'), $outs); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_paid_id')); ?>"><?php _e("Paid Items", 'AuctionTheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink(get_option('AuctionTheme_my_account_paid_ship_id')); ?>"><?php _e("Paid & Shipped", 'AuctionTheme'); ?></a></li>


                    </p>
            </li>


        </ul>
    </div>





    <?php
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_payments_page_url($subpage = '', $id = '') {
    $opt = get_option('AuctionTheme_my_account_payments_page_id');
    if (empty($subpage))
        $subpage = "home";

    $perm = AuctionTheme_using_permalinks();

    if ($perm)
        return get_permalink($opt) . "?pg=" . $subpage . (!empty($id) ? "&id=" . $id : '');

    return get_permalink($opt) . "&pg=" . $subpage . (!empty($id) ? "&id=" . $id : '');
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_add_history_log($tp, $reason, $amount, $uid, $uid2 = '') {
    global $wpdb;

    $tm = current_time('timestamp', 0);
    global $wpdb;
    $s = "insert into " . $wpdb->prefix . "auction_payment_transactions (tp,reason,amount,uid,datemade,uid2)
	values('$tp','$reason','$amount','$uid','$tm','$uid2')";
    $wpdb->query($s);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function auctionTheme_get_credits($uid) {
    $c = get_user_meta($uid, 'credits', true);
    if (empty($c)) {
        update_user_meta($uid, 'credits', "0");
        return 0;
    }

    return $c;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_post_nr_of_images($pid) {

    //---------------------
    // build the exclude list
    $exclude = array();

    $args = array(
        'order' => 'ASC',
        'post_type' => 'attachment',
        'post_parent' => get_the_ID(),
        'meta_key' => 'another_reserved1',
        'meta_value' => '1',
        'numberposts' => -1,
        'post_status' => null,
    );
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $url = $attachment->ID;
            array_push($exclude, $url);
        }
    }

    //-----------------


    $arr = array();

    $args = array(
        'order' => 'ASC',
        'orderby' => 'post_date',
        'post_type' => 'attachment',
        'post_parent' => $pid,
        'exclude' => $exclude,
        'post_mime_type' => 'image',
        'numberposts' => -1,
    );
    $i = 0;

    $attachments = get_posts($args);
    if ($attachments) {

        foreach ($attachments as $attachment) {

            $url = wp_get_attachment_url($attachment->ID);
            array_push($arr, $url);
        }
        return count($arr);
    }
    return 0;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_auction_primary_cat($pid) {
    $auction_terms = wp_get_object_terms($pid, 'auction_cat');
    if (is_array($auction_terms)) {
        return $auction_terms[0]->term_id;
    }

    return 0;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_images_cost_extra($pid) {
    $AuctionTheme_charge_fees_for_images = get_option('AuctionTheme_charge_fees_for_images');
    $auctionTheme_extra_image_charge = get_option('auctionTheme_extra_image_charge');


    if ($AuctionTheme_charge_fees_for_images == "yes") {
        $auctionTheme_nr_of_free_images = get_option('auctionTheme_nr_of_free_images');
        if (empty($auctionTheme_nr_of_free_images))
            $auctionTheme_nr_of_free_images = 1;

        $AuctionTheme_get_post_nr_of_images = AuctionTheme_get_post_nr_of_images($pid);

        $nr_imgs = $AuctionTheme_get_post_nr_of_images - $auctionTheme_nr_of_free_images;
        if ($nr_imgs > 0) {
            return $nr_imgs * $auctionTheme_extra_image_charge;
        }
    }

    return 0;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function get_auction_category_fields($catid, $pid = '') {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_custom_fields order by ordr asc";
    $r = $wpdb->get_results($s);

    $arr1 = array();
    $i = 0;

    foreach ($r as $row) {
        $ims = $row->id;
        $name = $row->name;
        $tp = $row->tp;

        if ($row->cate == 'all') {
            $arr1[$i]['id'] = $ims;
            $arr1[$i]['name'] = $name;
            $arr1[$i]['tp'] = $tp;
            $i++;
        } else {
            $se = "select * from " . $wpdb->prefix . "auction_custom_relations where custid='$ims'";
            $re = $wpdb->get_results($se);

            if (count($re) > 0)
                foreach ($re as $rowe) { // = mysql_fetch_object($re))
                    if (count($catid) > 0)
                        foreach ($catid as $id_of_cat) {

                            if ($rowe->catid == $id_of_cat) {
                                $flag_me = 1;
                                for ($k = 0; $k < count($arr1); $k++) {
                                    if ($arr1[$k]['id'] == $ims) {
                                        $flag_me = 0;
                                        break;
                                    }
                                }

                                if ($flag_me == 1) {
                                    $arr1[$i]['id'] = $ims;
                                    $arr1[$i]['name'] = $name;
                                    $arr1[$i]['tp'] = $tp;
                                    $i++;
                                }
                            }
                        }
                }
        }
    }

    $arr = array();
    $i = 0;

    for ($j = 0; $j < count($arr1); $j++) {
        $ids = $arr1[$j]['id'];
        $tp = $arr1[$j]['tp'];

        $arr[$i]['field_name'] = $arr1[$j]['name'];
        $arr[$i]['id'] = '<input type="hidden" value="' . $ids . '" name="custom_field_id[]" />';

        if ($tp == 1) {

            $teka = !empty($pid) ? get_post_meta($pid, 'custom_field_ID_' . $ids, true) : "";

            $arr[$i]['value'] = '<input class="do_input" type="text" size="30" name="custom_field_value_' . $ids . '" 
		value="' . (isset($_POST['custom_field_value_' . $ids]) ? $_POST['custom_field_value_' . $ids] : $teka ) . '" />';
        }

        if ($tp == 5) {

            $teka = !empty($pid) ? get_post_meta($pid, 'custom_field_ID_' . $ids, true) : "";
            $teka = $teka[0];
            $value = isset($_POST['custom_field_value_' . $ids]) ? $_POST['custom_field_value_' . $ids] : $teka;

            $arr[$i]['value'] = '<textarea rows="5" cols="40" name="custom_field_value_' . $ids . '">' . $value . '</textarea>';
        }

        if ($tp == 3) { //radio
            $arr[$i]['value'] = '';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = !empty($pid) ? get_post_meta($pid, 'custom_field_ID_' . $ids, true) : "";
                    $teka = $teka[0];

                    if (isset($_POST['custom_field_value_' . $ids])) {
                        if ($_POST['custom_field_value_' . $ids] == $row2->valval)
                            $value = 'checked="checked"';
                        else
                            $value = '';
                    }
                    elseif (!empty($pid)) {
                        $v = get_post_meta($pid, 'custom_field_ID_' . $ids, true);
                        if ($v == $row2->valval)
                            $value = 'checked="checked"';
                        else
                            $value = '';
                    }
                    else
                        $value = '';

                    $arr[$i]['value'] .= '<input type="radio" ' . $value . ' value="' . $row2->valval . '" name="custom_field_value_' . $ids . '"> ' . $row2->valval . '<br/>';
                }
        }


        if ($tp == 4) { //checkbox
            $arr[$i]['value'] = '';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = !empty($pid) ? get_post_meta($pid, 'custom_field_ID_' . $ids) : "";
                    $teka = $teka[0];
                    $teka_ch = '';

                    if (!empty($teka)) {
                        foreach ($teka as $te) {

                            if (trim($te) == trim($row2->valval)) {
                                $teka_ch = "checked='checked'";
                                break;
                            }
                        }
                    }
                    else
                        $teka_ch = '';


                    $teka_ch = isset($_POST['custom_field_value_' . $ids]) ? "checked='checked'" : $teka_ch;

                    $arr[$i]['value'] .= '<input type="checkbox" ' . $teka_ch . ' value="' . $row2->valval . '" name="custom_field_value_' . $ids . '[]"> ' . $row2->valval . '<br/>';
                }
        }

        if ($tp == 2) { //select
            $arr[$i]['value'] = '<select class="do_input" name="custom_field_value_' . $ids . '" />';

            $s2 = "select * from " . $wpdb->prefix . "auction_custom_options where custid='$ids' order by ordr ASC ";
            $r2 = $wpdb->get_results($s2);

            if (count($r2) > 0)
                foreach ($r2 as $row2) { // = mysql_fetch_object($r2))
                    $teka = !empty($pid) ? get_post_meta($pid, 'custom_field_ID_' . $ids) : "";

                    if (!empty($teka)) {
                        foreach ($teka as $te) {
                            if (trim($te) == trim($row2->valval)) {
                                $teka = "selected='selected'";
                                break;
                            }
                        }
                    }
                    else
                        $teka = '';

                    if (isset($_POST['custom_field_value_' . $ids]) && $_POST['custom_field_value_' . $ids] == $row2->valval)
                        $value = "selected='selected'";
                    else
                        $value = $teka;


                    $arr[$i]['value'] .= '<option ' . $value . ' value="' . $row2->valval . '">' . $row2->valval . '</option>';
                }
            $arr[$i]['value'] .= '</select>';
        }

        $i++;
    }

    return $arr;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_get_geo_coordinates($addr) {
    //http://maps.google.com/maps/geo?output=csv&q=Bucharest&key=	
    $key = get_option('auctionTheme_maps_api_key');
    $url = "http://maps.google.com/maps/geo?output=csv&q=" . urlencode($addr) . "&key=" . $key;

    $data = AuctionTheme_curl_get_data($url);
    $data = explode(",", $data);

    return $data;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_curl_get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */



add_action('wp_ajax_remove_from_watchlist', 'AuctionTheme_remove_from_watchlist');
add_action('wp_ajax_nopriv_remove_from_watchlist', 'AuctionTheme_remove_from_watchlist');

add_action('wp_ajax_add_to_watchlist', 'AuctionTheme_add_to_watchlist');
add_action('wp_ajax_nopriv_add_to_watchlist', 'AuctionTheme_add_to_watchlist');

function AuctionTheme_remove_from_watchlist() {
    $pid = $_POST['pid'];

    if (is_user_logged_in()):

        global $current_user;
        get_currentuserinfo();
        $uid = $current_user->ID;

        AuctionTheme_delete_pid_in_watchlist($pid, $uid);

        echo "removed-" . $uid . "-" . $pid . "-";

    else:

        echo "NO_LOGGED";

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_check_if_pid_is_in_watchlist($pid, $uid) {
    global $wpdb;
    $s = "select * from " . $wpdb->prefix . "auction_watchlist where pid='$pid' AND uid='$uid'";
    $r = $wpdb->get_results($s);

    if (count($r) == 0)
        return false;
    return true;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_add_pid_in_watchlist($pid, $uid) {
    global $wpdb;
    $s = "insert into " . $wpdb->prefix . "auction_watchlist (pid,uid) values('$pid','$uid')";
    $wpdb->query($s);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_delete_pid_in_watchlist($pid, $uid) {
    global $wpdb;
    $s = "delete from " . $wpdb->prefix . "auction_watchlist where pid='$pid' AND uid='$uid'";
    $wpdb->query($s);
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_add_to_watchlist() {
    $pid = $_POST['pid'];

    if (is_user_logged_in()):

        global $current_user;
        get_currentuserinfo();
        $uid = $current_user->ID;

        if (AuctionTheme_check_if_pid_is_in_watchlist($pid, $uid) == false)
            AuctionTheme_add_pid_in_watchlist($pid, $uid);

        echo "added-" . $uid . "-" . $pid . "-";

    else:

        echo "NO_LOGGED";

    endif;
}

//--------------------------------------------------------------------

if (isset($_GET['confirm_message_deletion'])) {

    $using_perm = AuctionTheme_using_permalinks();

    if ($using_perm)
        $privurl_m = get_permalink(get_option('AuctionTheme_my_account_priv_mess_page_id')) . "?";
    else
        $privurl_m = get_bloginfo('siteurl') . "/?page_id=" . get_option('AuctionTheme_my_account_priv_mess_page_id') . "&";

    $return = $_GET['return'];
    $messid = $_GET['id'];

    global $wpdb, $current_user;
    get_currentuserinfo();
    $uid = $current_user->ID;

    $s = "select * from " . $wpdb->prefix . "auction_pm where id='$messid' AND (user='$uid' OR initiator='$uid')";
    $r = $wpdb->get_results($s);

    if (count($r) > 0) {
        $row = $r[0];

        if ($row->initiator == $uid) {
            $s = "update " . $wpdb->prefix . "auction_pm set show_to_source='0' where id='$messid'";
            $wpdb->query($s);
        } else {
            $s = "update " . $wpdb->prefix . "auction_pm set show_to_destination='0' where id='$messid'";
            $wpdb->query($s);
        }

        if ($return == "inbox")
            wp_redirect($privurl_m . "priv_act=inbox/");
        else if ($return == "outbox")
            wp_redirect($privurl_m . "priv_act=sent-items/");
        else if ($return == "home")
            wp_redirect($privurl_m . "priv_act=private-messages");
        else
            wp_redirect(AuctionTheme_my_account_link());
    }
    else
        wp_redirect(AuctionTheme_my_account_link());
}

//====================== EMAILS -------------------------------------------
function AuctionTheme_replace_stuff_for_me($find, $replace, $subject) {
    $i = 0;
    foreach ($find as $item) {
        $replace_with = $replace[$i];
        $subject = str_replace($item, $replace_with, $subject);
        $i++;
    }

    return $subject;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctiontTheme_login_url() {
    return get_bloginfo('siteurl') . '/wp-login.php';
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_posted_item_not_approved($pid) {
    $enable = get_option('AuctionTheme_new_item_email_not_approved_enable');
    $subject = get_option('AuctionTheme_new_item_email_not_approved_subject');
    $message = get_option('AuctionTheme_new_item_email_not_approved_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctiontTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_posted_item_not_approved_admin';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_posted_item_not_approved_admin($pid) {
    $enable = get_option('AuctionTheme_new_item_email_not_approve_admin_enable');
    $subject = get_option('AuctionTheme_new_item_email_not_approve_admin_subject');
    $message = get_option('AuctionTheme_new_item_email_not_approve_admin_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_posted_item_not_approved_admin';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        $email = get_bloginfo('admin_email');
        AuctionTheme_send_email($email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_posted_item_approved($pid) {
    $enable = get_option('AuctionTheme_new_item_email_approved_enable');
    $subject = get_option('AuctionTheme_new_item_email_approved_subject');
    $message = get_option('AuctionTheme_new_item_email_approved_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $post = get_post($pid);
        $item_name = $post->post_title;
        $item_link = get_permalink($pid);

        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $item_name, $item_link);

        $tag = 'AuctionTheme_send_email_posted_item_approved';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        $email = $user->user_email;
        AuctionTheme_send_email($email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_posted_item_approved_admin($pid) {
    $enable = get_option('AuctionTheme_new_item_email_approve_admin_enable');
    $subject = get_option('AuctionTheme_new_item_email_approve_admin_subject');
    $message = get_option('AuctionTheme_new_item_email_approve_admin_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_posted_item_approved_admin';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        $email = get_bloginfo('admin_email');
        AuctionTheme_send_email($email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_bid_item_owner($pid, $uid, $bid) {
    $enable = get_option('AuctionTheme_bid_item_owner_email_enable');
    $subject = get_option('AuctionTheme_bid_item_owner_email_subject');
    $message = get_option('AuctionTheme_bid_item_owner_email_message');



    if ($enable != "no"):

        $bidder = get_userdata($uid);
        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));
        $bid_val = AuctionTheme_get_show_price($bid);
        $bidder_username = $bidder->user_login;
        $author = get_userdata($post->post_author);


        $find = array('##username##', '##bid_value##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##bidder_username##');
        $replace = array($user->user_login, $bid_val, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $bidder_username);

        $tag = 'AuctionTheme_send_email_when_bid_item_owner';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($author->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_bid_item_outbid($pid, $uid, $bid) {
    $enable = get_option('AuctionTheme_bid_item_outbid_email_enable');
    $subject = get_option('AuctionTheme_bid_item_outbid_email_subject');
    $message = get_option('AuctionTheme_bid_item_outbid_email_message');



    if ($enable != "no"):

        $bidder = get_userdata($uid);
        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));
        $bid_val = AuctionTheme_get_show_price($bid);
        $bidder_username = $bidder->user_login;
        $author = get_userdata($post->post_author);


        $find = array('##username##', '##bid_value##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##bidder_username##');
        $replace = array($user->user_login, $bid_val, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $bidder_username);

        $tag = 'AuctionTheme_send_email_when_bid_item_outbid';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($author->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_item_is_paid_buyer($pid, $bid_id) { //received by buyer
    $enable = get_option('AuctionTheme_paid_auction_buyer_email_enable');
    $subject = get_option('AuctionTheme_paid_auction_buyer_email_subject');
    $message = get_option('AuctionTheme_paid_auction_buyer_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($row->uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));



        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_item_is_paid_buyer';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($buyer->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_item_is_paid_seller($pid, $bid_id) { //received by seller
    $enable = get_option('AuctionTheme_paid_auction_seller_email_enable');
    $subject = get_option('AuctionTheme_paid_auction_seller_email_subject');
    $message = get_option('AuctionTheme_paid_auction_seller_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($row->uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_item_is_paid_seller';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($seller->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_item_is_shipped_seller($pid, $bid_id) { //received by buyer
    $enable = get_option('AuctionTheme_ship_auction_seller_email_enable');
    $subject = get_option('AuctionTheme_ship_auction_seller_email_subject');
    $message = get_option('AuctionTheme_ship_auction_seller_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($row->uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_item_is_shipped_seller';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($seller->user_email, $subject, $message);

    endif;
}

function AuctionTheme_send_email_when_item_is_shipped_buyer($pid, $bid_id) { //received by buyer
    $enable = get_option('AuctionTheme_ship_auction_buyer_email_enable');
    $subject = get_option('AuctionTheme_ship_auction_buyer_email_subject');
    $message = get_option('AuctionTheme_ship_auction_buyer_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($row->uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_item_is_shipped_buyer';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($buyer->user_email, $subject, $message);

    endif;
}

function AuctionTheme_send_email_when_review_needs_to_be_awarded($pid, $rated_user_uid, $awarding_user_uid) { //received by buyer
    $enable = get_option('AuctionTheme_review_to_award_email_enable');
    $subject = get_option('AuctionTheme_review_to_award_email_subject');
    $message = get_option('AuctionTheme_review_to_award_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $rated_user = get_userdata($rated_user_uid);
        $awarding_user = get_userdata($awarding_user_uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##rated_user##', '##awarding_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($rated_user->user_login, $awarding_user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_review_needs_to_be_awarded';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($awarding_user_uid->user_email, $subject, $message);

    endif;
}

function AuctionTheme_send_email_when_review_has_been_awarded($pid, $rated_user_uid, $awarding_user_uid) { //received by buyer
    $enable = get_option('AuctionTheme_review_received_email_enable');
    $subject = get_option('AuctionTheme_review_received_email_subject');
    $message = get_option('AuctionTheme_review_received_email_message');



    if ($enable != "no"):

        global $wpdb;
        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];

        $post = get_post($pid);
        $rated_user = get_userdata($rated_user_uid);
        $awarding_user = get_userdata($awarding_user_uid);

        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));


        $find = array('##rated_user##', '##awarding_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##');
        $replace = array($rated_user->user_login, $awarding_user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid));

        $tag = 'AuctionTheme_send_email_when_review_has_been_awarded';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($rated_user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_when_bid_item_bidder($pid, $uid, $bid) {
    $enable = get_option('AuctionTheme_bid_item_bidder_email_enable');
    $subject = get_option('AuctionTheme_bid_item_bidder_email_subject');
    $message = get_option('AuctionTheme_bid_item_bidder_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));
        $bid_val = AuctionTheme_get_show_price($bid);

        $find = array('##username##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##bid_value##');
        $replace = array($user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $bid_val);


        //---------------------------------------------

        $tag = 'AuctionTheme_send_email_when_bid_item_bidder';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);


        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_on_no_win_owner($pid) {
    $enable = get_option('AuctionTheme_on_no_win_email_enable');
    $subject = get_option('AuctionTheme_on_no_win_email_subject');
    $message = get_option('AuctionTheme_on_no_win_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($winner_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $winner_bid = '';

        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##winner_bid_value##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $winner_bid);

        $tag = 'AuctionTheme_send_email_on_win_to_bidder';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //--------------------------------------

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

//-------------------------------------

function AuctionTheme_send_email_on_buy_now_auction_buyer($pid, $buyer_uid) { //receives buyer
    $enable = get_option('AuctionTheme_buy_now_auction_buyer_email_enable');
    $subject = get_option('AuctionTheme_buy_now_auction_buyer_email_subject');
    $message = get_option('AuctionTheme_buy_now_auction_buyer_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($buyer_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $winner_bid = '';

        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##winner_bid_value##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $winner_bid);

        $tag = 'AuctionTheme_send_email_on_buy_now_auction_buyer';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //--------------------------------------

        AuctionTheme_send_email($buyer->user_email, $subject, $message);

    endif;
}

function AuctionTheme_send_email_on_buy_now_auction_seller($pid, $buyer_uid) { //receives seller
    $enable = get_option('AuctionTheme_buy_now_auction_seller_email_enable');
    $subject = get_option('AuctionTheme_buy_now_auction_seller_email_subject');
    $message = get_option('AuctionTheme_buy_now_auction_seller_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $seller = get_userdata($post->post_author);
        $buyer = get_userdata($buyer_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $winner_bid = '';

        $find = array('##seller_user##', '##buyer_user##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##winner_bid_value##');
        $replace = array($seller->user_login, $buyer->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $winner_bid);

        $tag = 'AuctionTheme_send_email_on_buy_now_auction_seller';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //--------------------------------------

        AuctionTheme_send_email($seller->user_email, $subject, $message);

    endif;
}

function AuctionTheme_send_email_on_win_to_bidder($pid, $winner_uid) {
    $enable = get_option('AuctionTheme_won_item_winner_email_enable');
    $subject = get_option('AuctionTheme_won_item_winner_email_subject');
    $message = get_option('AuctionTheme_won_item_winner_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($winner_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $winner_bid = '';

        $find = array('##username##', '##username_email##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##winner_bid_value##');
        $replace = array($user->user_login, $user->user_email, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid), $winner_bid);

        $tag = 'AuctionTheme_send_email_on_win_to_bidder';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //--------------------------------------

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_on_win_to_owner($pid, $winner_uid, $bid_id) {
    $enable = get_option('AuctionTheme_won_item_owner_email_enable');
    $subject = get_option('AuctionTheme_won_item_owner_email_subject');
    $message = get_option('AuctionTheme_won_item_owner_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($post->post_author);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $winner_bid_username = '';
        $winner_bid_value = '';

        global $wpdb;

        $s = "select * from " . $wpdb->prefix . "auction_bids where id='$bid_id'";
        $r = $wpdb->get_results($s);
        $row = $r[0];


        $winner_bid_username = get_userdata($winner_uid);
        $winner_bid_username = $winner_bid_username->user_login;
        $winner_bid_value = auctionTheme_get_show_price($row->bid);

        //--------------------------------------------------------------------------

        $find = array('##username##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##', '##winner_bid_value##', '##winner_bid_username##');
        $replace = array($user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid),
            $winner_bid_value, $winner_bid_username);

        $tag = 'AuctionTheme_send_email_on_win_to_owner';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_on_win_to_loser($pid, $loser_uid) {
    $enable = get_option('AuctionTheme_won_item_loser_email_enable');
    $subject = get_option('AuctionTheme_won_item_loser_email_subject');
    $message = get_option('AuctionTheme_won_item_loser_email_message');

    if ($enable != "no"):

        $post = get_post($pid);
        $user = get_userdata($loser_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));

        $user_bid_value = '';
        $winner_bid_username = '';
        $winner_bid_value = '';

        $find = array('##username##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##', '##item_name##', '##item_link##',
            '##user_bid_value##', '##winner_bid_username##', '##winner_bid_value##');
        $replace = array($user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url, $post->post_title, get_permalink($pid),
            $user_bid_value, $winner_bid_username, $winner_bid_value);

        $tag = 'AuctionTheme_send_email_on_win_to_loser';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}

/* * ***************************************************************************
 *
 * 	Function - AuctionTheme -
 *
 * *************************************************************************** */

function AuctionTheme_send_email_on_priv_mess_received($sender_uid, $receiver_uid) {
    $enable = get_option('AuctionTheme_priv_mess_received_email_enable');
    $subject = get_option('AuctionTheme_priv_mess_received_email_subject');
    $message = get_option('AuctionTheme_priv_mess_received_email_message');

    if ($enable != "no"):

        $user = get_userdata($receiver_uid);
        $site_login_url = AuctionTheme_login_url();
        $site_name = get_bloginfo('name');
        $account_url = get_permalink(get_option('AuctionTheme_my_account_page_id'));
        $sndr = get_userdata($sender_uid);

        $find = array('##sender_username##', '##receiver_username##', '##site_login_url##', '##your_site_name##', '##your_site_url##', '##my_account_url##');
        $replace = array($sndr->user_login, $user->user_login, $site_login_url, $site_name, get_bloginfo('siteurl'), $account_url);

        $tag = 'AuctionTheme_send_email_on_priv_mess_received';
        $find = apply_filters($tag . '_find', $find);
        $replace = apply_filters($tag . '_replace', $replace);

        $message = AuctionTheme_replace_stuff_for_me($find, $replace, $message);
        $subject = AuctionTheme_replace_stuff_for_me($find, $replace, $subject);

        //---------------------------------------------

        AuctionTheme_send_email($user->user_email, $subject, $message);

    endif;
}
?>